Skip to content

add token revoke #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func (m *JwtAuthMiddleware) ParseAuthHeader(ctx *gin.Context) error {
m.tokenString = jwtArr[1]
return nil
}
func (m *JwtAuthMiddleware) GetTokenStr() *string {
return &m.tokenString
}

func (m *JwtAuthMiddleware) ParseJwtToken(ctx *gin.Context) error {
//todo move to new res
Expand Down
1 change: 1 addition & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Middleware interface {
ParseJwtToken(ctx *gin.Context) error
GetUser(ctx *gin.Context) (*gorf.BaseUser, error)
Authenticate(ctx *gin.Context) (*gorf.BaseUser, error)
GetTokenStr() *string
}

func AuthenticationRequiredMiddleware(ctx *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type authSettings struct {
UserPool string
Region string
UserObjId string
AuthMiddleware *JwtAuthMiddleware
AuthMiddleware Middleware
JwkRes *JwkRes
}

Expand Down
1 change: 1 addition & 0 deletions urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import "github.com/gin-gonic/gin"

func Urls(r *gin.Engine) {
r.POST("auth/login", UserLogin)
r.GET("auth/logout", AuthenticationRequiredMiddleware, UserLogout)
r.GET("auth/protected-api", AuthenticationRequiredMiddleware, ProtectedApi)
}
29 changes: 29 additions & 0 deletions views.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"fmt"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider/types"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -44,7 +45,35 @@ func UserLogin(ctx *gin.Context) {
}

func ProtectedApi(ctx *gin.Context) {
//todo fix format
gorf.Response(ctx, gin.H{
"Status": "ok",
})
}

func UserLogout(ctx *gin.Context) {

err := Settings.AuthMiddleware.ParseAuthHeader(ctx)
if err != nil {
gorf.BadRequest(ctx, "error on parsing header", err)
return
}

tokenInput := &cognitoidentityprovider.RevokeTokenInput{
ClientId: &Settings.ClientId,
Token: Settings.AuthMiddleware.GetTokenStr(),
}

result, err := client.RevokeToken(cognitoCtx, tokenInput)
if err != nil {
gorf.BadRequest(ctx, "failed to revoke token", err)
return
}

fmt.Println(result.ResultMetadata)

//fix format
gorf.Response(ctx, gin.H{
"Status": "logout successfully",
})
}