|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/authorizerdev/authorizer/server/constants" |
| 8 | + "github.com/authorizerdev/authorizer/server/envstore" |
| 9 | + "github.com/authorizerdev/authorizer/server/sessionstore" |
| 10 | + "github.com/gin-gonic/gin" |
| 11 | +) |
| 12 | + |
| 13 | +// Revoke handler to revoke refresh token |
| 14 | +func RevokeHandler() gin.HandlerFunc { |
| 15 | + return func(gc *gin.Context) { |
| 16 | + var reqBody map[string]string |
| 17 | + if err := gc.BindJSON(&reqBody); err != nil { |
| 18 | + gc.JSON(http.StatusBadRequest, gin.H{ |
| 19 | + "error": "error_binding_json", |
| 20 | + "error_description": err.Error(), |
| 21 | + }) |
| 22 | + return |
| 23 | + } |
| 24 | + // get fingerprint hash |
| 25 | + refreshToken := strings.TrimSpace(reqBody["refresh_token"]) |
| 26 | + clientID := strings.TrimSpace(reqBody["client_id"]) |
| 27 | + |
| 28 | + if clientID == "" { |
| 29 | + gc.JSON(http.StatusBadRequest, gin.H{ |
| 30 | + "error": "client_id_required", |
| 31 | + "error_description": "The client id is required", |
| 32 | + }) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + if clientID != envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyClientID) { |
| 37 | + gc.JSON(http.StatusBadRequest, gin.H{ |
| 38 | + "error": "invalid_client_id", |
| 39 | + "error_description": "The client id is invalid", |
| 40 | + }) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + sessionstore.RemoveState(refreshToken) |
| 45 | + |
| 46 | + gc.JSON(http.StatusOK, gin.H{ |
| 47 | + "message": "Token revoked successfully", |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
0 commit comments