Files
authorizer/server/handlers/logout.go

62 lines
1.5 KiB
Go
Raw Normal View History

2022-03-04 00:36:27 +05:30
package handlers
import (
2022-06-12 00:27:21 +05:30
"encoding/json"
2022-03-04 00:36:27 +05:30
"net/http"
2022-03-08 21:32:42 +05:30
"strings"
2022-03-04 00:36:27 +05:30
2022-05-23 11:52:51 +05:30
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
2022-03-04 00:36:27 +05:30
"github.com/authorizerdev/authorizer/server/cookie"
"github.com/authorizerdev/authorizer/server/crypto"
2022-05-27 23:20:38 +05:30
"github.com/authorizerdev/authorizer/server/memorystore"
2022-06-12 00:27:21 +05:30
"github.com/authorizerdev/authorizer/server/token"
2022-03-04 00:36:27 +05:30
)
2022-03-08 18:49:42 +05:30
// Handler to logout user
2022-03-04 00:36:27 +05:30
func LogoutHandler() gin.HandlerFunc {
return func(gc *gin.Context) {
2022-03-08 22:41:33 +05:30
redirectURL := strings.TrimSpace(gc.Query("redirect_uri"))
2022-03-04 00:36:27 +05:30
// get fingerprint hash
fingerprintHash, err := cookie.GetSession(gc)
if err != nil {
2022-05-25 12:30:22 +05:30
log.Debug("Failed to get session: ", err)
2022-03-04 00:36:27 +05:30
gc.JSON(http.StatusUnauthorized, gin.H{
"error": err.Error(),
})
return
}
decryptedFingerPrint, err := crypto.DecryptAES(fingerprintHash)
if err != nil {
2022-05-25 12:30:22 +05:30
log.Debug("Failed to decrypt fingerprint: ", err)
2022-03-04 00:36:27 +05:30
gc.JSON(http.StatusUnauthorized, gin.H{
"error": err.Error(),
})
return
}
2022-06-12 00:27:21 +05:30
var sessionData token.SessionData
err = json.Unmarshal([]byte(decryptedFingerPrint), &sessionData)
2022-05-27 23:20:38 +05:30
if err != nil {
2022-06-12 00:27:21 +05:30
log.Debug("Failed to decrypt fingerprint: ", err)
gc.JSON(http.StatusUnauthorized, gin.H{
"error": err.Error(),
})
return
2022-05-27 23:20:38 +05:30
}
2022-06-12 00:27:21 +05:30
memorystore.Provider.DeleteUserSession(sessionData.Subject, sessionData.Nonce)
2022-03-04 00:36:27 +05:30
cookie.DeleteSession(gc)
2022-03-08 21:32:42 +05:30
if redirectURL != "" {
2022-03-08 22:41:33 +05:30
gc.Redirect(http.StatusFound, redirectURL)
2022-03-08 21:32:42 +05:30
} else {
gc.JSON(http.StatusOK, gin.H{
"message": "Logged out successfully",
})
}
2022-03-04 00:36:27 +05:30
}
}