authorizer/server/handlers/logout.go

48 lines
1.0 KiB
Go
Raw Permalink Normal View History

2022-03-03 19:06:27 +00:00
package handlers
import (
"net/http"
2022-03-08 16:02:42 +00:00
"strings"
2022-03-03 19:06:27 +00:00
"github.com/authorizerdev/authorizer/server/cookie"
"github.com/authorizerdev/authorizer/server/crypto"
"github.com/authorizerdev/authorizer/server/sessionstore"
"github.com/gin-gonic/gin"
)
2022-03-08 13:19:42 +00:00
// Handler to logout user
2022-03-03 19:06:27 +00:00
func LogoutHandler() gin.HandlerFunc {
return func(gc *gin.Context) {
2022-03-08 17:11:33 +00:00
redirectURL := strings.TrimSpace(gc.Query("redirect_uri"))
2022-03-03 19:06:27 +00:00
// get fingerprint hash
fingerprintHash, err := cookie.GetSession(gc)
if err != nil {
gc.JSON(http.StatusUnauthorized, gin.H{
"error": err.Error(),
})
return
}
decryptedFingerPrint, err := crypto.DecryptAES(fingerprintHash)
if err != nil {
gc.JSON(http.StatusUnauthorized, gin.H{
"error": err.Error(),
})
return
}
fingerPrint := string(decryptedFingerPrint)
sessionstore.RemoveState(fingerPrint)
cookie.DeleteSession(gc)
2022-03-08 16:02:42 +00:00
if redirectURL != "" {
2022-03-08 17:11:33 +00:00
gc.Redirect(http.StatusFound, redirectURL)
2022-03-08 16:02:42 +00:00
} else {
gc.JSON(http.StatusOK, gin.H{
"message": "Logged out successfully",
})
}
2022-03-03 19:06:27 +00:00
}
}