-
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.
Added Authentication middleware & added authorization logic in all APIs
- Loading branch information
1 parent
c40d624
commit 08e18ea
Showing
7 changed files
with
111 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package api | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/ashiqYousuf/sbank/token" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
const ( | ||
authorizationHeaderKey = "authorization" | ||
authorizationTypeBearer = "bearer" | ||
authorizationPayloadKey = "authorization_payload" | ||
) | ||
|
||
// Using closure pattern to inject dependencies | ||
// Or we can make middleware funcs methods against server struct | ||
func authMiddleware(tokenMaker token.Maker) gin.HandlerFunc { | ||
return func(ctx *gin.Context) { | ||
authorizationHeader := ctx.GetHeader(authorizationHeaderKey) | ||
if len(authorizationHeader) == 0 { | ||
err := errors.New("authorization header is not provided") | ||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
fields := strings.Fields(authorizationHeader) | ||
if len(fields) < 2 { | ||
err := errors.New("invalid authorization header format") | ||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
authorizationType := strings.ToLower(fields[0]) | ||
if authorizationType != authorizationTypeBearer { | ||
err := fmt.Errorf("unsupported authorization type %s", authorizationType) | ||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
accessToken := fields[1] | ||
payload, err := tokenMaker.VerifyToken(accessToken) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
// Means a valid token is passed, store payload (user info) in context | ||
ctx.Set(authorizationPayloadKey, payload) | ||
ctx.Next() | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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