diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-08-01 21:08:31 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-08-01 21:08:31 +0900 |
| commit | 6767acd3d9cc2cf5b778048ec6339b8c9123fbb5 (patch) | |
| tree | dc436435b043158275501eab4b79e5a64cf013d9 /backend/api/handler_wrapper.go | |
| parent | 5e6775c9c1efbbd3b08363ffda421a5996dc7143 (diff) | |
| download | phperkaigi-2025-albatross-6767acd3d9cc2cf5b778048ec6339b8c9123fbb5.tar.gz phperkaigi-2025-albatross-6767acd3d9cc2cf5b778048ec6339b8c9123fbb5.tar.zst phperkaigi-2025-albatross-6767acd3d9cc2cf5b778048ec6339b8c9123fbb5.zip | |
refactor(backend): wrap ApiHandler with user authentication
Diffstat (limited to 'backend/api/handler_wrapper.go')
| -rw-r--r-- | backend/api/handler_wrapper.go | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/backend/api/handler_wrapper.go b/backend/api/handler_wrapper.go new file mode 100644 index 0000000..37a199b --- /dev/null +++ b/backend/api/handler_wrapper.go @@ -0,0 +1,134 @@ +// Code generated by go generate; DO NOT EDIT. + +package api + +import ( + "context" + "errors" + "strings" + + "github.com/nsfisis/iosdc-japan-2024-albatross/backend/auth" + "github.com/nsfisis/iosdc-japan-2024-albatross/backend/db" +) + +var _ StrictServerInterface = (*ApiHandlerWrapper)(nil) + +type ApiHandlerWrapper struct { + innerHandler ApiHandler +} + +func NewHandler(queries *db.Queries, hubs GameHubsInterface) *ApiHandlerWrapper { + return &ApiHandlerWrapper{ + innerHandler: ApiHandler{ + q: queries, + hubs: hubs, + }, + } +} + +func parseJWTClaimsFromAuthorizationHeader(authorization string) (*auth.JWTClaims, error) { + const prefix = "Bearer " + if !strings.HasPrefix(authorization, prefix) { + return nil, errors.New("invalid authorization header") + } + token := authorization[len(prefix):] + claims, err := auth.ParseJWT(token) + if err != nil { + return nil, err + } + return claims, nil +} + +func (h *ApiHandlerWrapper) AdminGetGame(ctx context.Context, request AdminGetGameRequestObject) (AdminGetGameResponseObject, error) { + user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization) + if err != nil { + return AdminGetGame401JSONResponse{ + Message: "Unauthorized", + }, nil + } + if !user.IsAdmin { + return AdminGetGame403JSONResponse{ + Message: "Forbidden", + }, nil + } + return h.innerHandler.AdminGetGame(ctx, request, user) +} + +func (h *ApiHandlerWrapper) AdminGetGames(ctx context.Context, request AdminGetGamesRequestObject) (AdminGetGamesResponseObject, error) { + user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization) + if err != nil { + return AdminGetGames401JSONResponse{ + Message: "Unauthorized", + }, nil + } + if !user.IsAdmin { + return AdminGetGames403JSONResponse{ + Message: "Forbidden", + }, nil + } + return h.innerHandler.AdminGetGames(ctx, request, user) +} + +func (h *ApiHandlerWrapper) AdminGetUsers(ctx context.Context, request AdminGetUsersRequestObject) (AdminGetUsersResponseObject, error) { + user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization) + if err != nil { + return AdminGetUsers401JSONResponse{ + Message: "Unauthorized", + }, nil + } + if !user.IsAdmin { + return AdminGetUsers403JSONResponse{ + Message: "Forbidden", + }, nil + } + return h.innerHandler.AdminGetUsers(ctx, request, user) +} + +func (h *ApiHandlerWrapper) AdminPutGame(ctx context.Context, request AdminPutGameRequestObject) (AdminPutGameResponseObject, error) { + user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization) + if err != nil { + return AdminPutGame401JSONResponse{ + Message: "Unauthorized", + }, nil + } + if !user.IsAdmin { + return AdminPutGame403JSONResponse{ + Message: "Forbidden", + }, nil + } + return h.innerHandler.AdminPutGame(ctx, request, user) +} + +func (h *ApiHandlerWrapper) GetGame(ctx context.Context, request GetGameRequestObject) (GetGameResponseObject, error) { + user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization) + if err != nil { + return GetGame401JSONResponse{ + Message: "Unauthorized", + }, nil + } + return h.innerHandler.GetGame(ctx, request, user) +} + +func (h *ApiHandlerWrapper) GetGames(ctx context.Context, request GetGamesRequestObject) (GetGamesResponseObject, error) { + user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization) + if err != nil { + return GetGames401JSONResponse{ + Message: "Unauthorized", + }, nil + } + return h.innerHandler.GetGames(ctx, request, user) +} + +func (h *ApiHandlerWrapper) GetToken(ctx context.Context, request GetTokenRequestObject) (GetTokenResponseObject, error) { + user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization) + if err != nil { + return GetToken401JSONResponse{ + Message: "Unauthorized", + }, nil + } + return h.innerHandler.GetToken(ctx, request, user) +} + +func (h *ApiHandlerWrapper) PostLogin(ctx context.Context, request PostLoginRequestObject) (PostLoginResponseObject, error) { + return h.innerHandler.PostLogin(ctx, request) +} |
