authorizer/server/handlers/revoke_refresh_token.go

77 lines
2.2 KiB
Go
Raw Normal View History

2022-03-08 13:19:42 +00:00
package handlers
import (
"net/http"
"strings"
2022-05-23 06:22:51 +00:00
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
2022-03-08 13:19:42 +00:00
"github.com/authorizerdev/authorizer/server/constants"
2022-05-27 17:50:38 +00:00
"github.com/authorizerdev/authorizer/server/memorystore"
2022-06-11 18:57:21 +00:00
"github.com/authorizerdev/authorizer/server/token"
2022-03-08 13:19:42 +00:00
)
// RevokeRefreshTokenHandler handler to revoke refresh token
func RevokeRefreshTokenHandler() gin.HandlerFunc {
2022-03-08 13:19:42 +00:00
return func(gc *gin.Context) {
var reqBody map[string]string
if err := gc.BindJSON(&reqBody); err != nil {
2022-05-23 06:22:51 +00:00
log.Debug("Error binding JSON: ", err)
2022-03-08 13:19:42 +00:00
gc.JSON(http.StatusBadRequest, gin.H{
"error": "error_binding_json",
"error_description": err.Error(),
})
return
}
2024-04-02 09:55:11 +00:00
// get client ID
clientID := strings.TrimSpace(reqBody["client_id"]) // kept for backward compatibility // else we expect to be present as header
if clientID == "" {
clientID = gc.Request.Header.Get("x-authorizer-client-id")
}
2022-03-08 13:19:42 +00:00
// get fingerprint hash
refreshToken := strings.TrimSpace(reqBody["refresh_token"])
if clientID == "" {
2022-05-23 06:22:51 +00:00
log.Debug("Client ID is empty")
2022-03-08 13:19:42 +00:00
gc.JSON(http.StatusBadRequest, gin.H{
"error": "client_id_required",
"error_description": "The client id is required",
})
return
}
2022-05-29 11:52:46 +00:00
if client, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyClientID); client != clientID || err != nil {
2022-05-25 07:00:22 +00:00
log.Debug("Client ID is invalid: ", clientID)
2022-03-08 13:19:42 +00:00
gc.JSON(http.StatusBadRequest, gin.H{
"error": "invalid_client_id",
"error_description": "The client id is invalid",
})
return
}
2022-06-11 18:57:21 +00:00
claims, err := token.ParseJWTToken(refreshToken)
if err != nil {
log.Debug("Client ID is invalid: ", clientID)
gc.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
"error_description": "Failed to parse jwt",
})
return
}
userID := claims["sub"].(string)
loginMethod := claims["login_method"]
sessionToken := userID
if loginMethod != nil && loginMethod != "" {
sessionToken = loginMethod.(string) + ":" + userID
}
memorystore.Provider.DeleteUserSession(sessionToken, claims["nonce"].(string))
2022-03-08 13:19:42 +00:00
gc.JSON(http.StatusOK, gin.H{
"message": "Token revoked successfully",
})
}
}