-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HTTP API, token based auth and middleware
- Loading branch information
1 parent
12b85fc
commit f57b2f5
Showing
35 changed files
with
1,047 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package api | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/viralparmarme/simple-bank/token" | ||
) | ||
|
||
const ( | ||
authHeaderKey = "authorization" | ||
authTypeBearer = "bearer" | ||
authPayloadKey = "authorization_payload" | ||
) | ||
|
||
func authMiddleware(tokenMaker token.Maker) gin.HandlerFunc { | ||
return func(ctx *gin.Context) { | ||
authHeader := ctx.GetHeader(authHeaderKey) | ||
if len(authHeader) == 0 { | ||
err := errors.New("authorization header is not provided") | ||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
fields := strings.Fields(authHeader) | ||
if len(fields) < 2 { | ||
err := errors.New("authorization header is invalid") | ||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
authType := strings.ToLower(fields[0]) | ||
if authType != authTypeBearer { | ||
err := errors.New("authorization header is unsupported") | ||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
accessToken := fields[1] | ||
payload, err := tokenMaker.VerifyToken(accessToken) | ||
if err != nil { | ||
err := errors.New("authorization header format is invalid") | ||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.Set(authPayloadKey, payload) | ||
ctx.Next() | ||
} | ||
} |
Oops, something went wrong.