authorizer/server/handlers/logout.go

55 lines
1.2 KiB
Go
Raw 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
2022-05-23 06:22:51 +00:00
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
2022-03-03 19:06:27 +00:00
"github.com/authorizerdev/authorizer/server/cookie"
"github.com/authorizerdev/authorizer/server/crypto"
2022-05-27 17:50:38 +00:00
"github.com/authorizerdev/authorizer/server/memorystore"
2022-03-03 19:06:27 +00:00
)
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 {
2022-05-25 07:00:22 +00:00
log.Debug("Failed to get session: ", err)
2022-03-03 19:06:27 +00:00
gc.JSON(http.StatusUnauthorized, gin.H{
"error": err.Error(),
})
return
}
decryptedFingerPrint, err := crypto.DecryptAES(fingerprintHash)
if err != nil {
2022-05-25 07:00:22 +00:00
log.Debug("Failed to decrypt fingerprint: ", err)
2022-03-03 19:06:27 +00:00
gc.JSON(http.StatusUnauthorized, gin.H{
"error": err.Error(),
})
return
}
fingerPrint := string(decryptedFingerPrint)
2022-05-27 17:50:38 +00:00
err = memorystore.Provider.RemoveState(fingerPrint)
if err != nil {
log.Debug("Failed to remove state: ", err)
}
2022-03-03 19:06:27 +00:00
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
}
}