authorizer/server/handlers/verify_email.go

117 lines
3.3 KiB
Go
Raw Normal View History

package handlers
import (
"net/http"
"strconv"
"strings"
"time"
"github.com/authorizerdev/authorizer/server/cookie"
2021-07-23 16:27:44 +00:00
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/sessionstore"
"github.com/authorizerdev/authorizer/server/token"
2021-07-23 16:27:44 +00:00
"github.com/authorizerdev/authorizer/server/utils"
"github.com/gin-gonic/gin"
)
2022-01-17 06:02:13 +00:00
// VerifyEmailHandler handles the verify email route.
// It verifies email based on JWT token in query string
2021-07-23 08:55:32 +00:00
func VerifyEmailHandler() gin.HandlerFunc {
return func(c *gin.Context) {
errorRes := gin.H{
"error": "invalid_token",
}
tokenInQuery := c.Query("token")
if tokenInQuery == "" {
c.JSON(400, errorRes)
return
}
verificationRequest, err := db.Provider.GetVerificationRequestByToken(tokenInQuery)
if err != nil {
errorRes["error_description"] = err.Error()
c.JSON(400, errorRes)
return
}
// verify if token exists in db
2022-03-02 12:12:31 +00:00
hostname := utils.GetHost(c)
claim, err := token.ParseJWTToken(tokenInQuery, hostname, verificationRequest.Nonce, verificationRequest.Email)
if err != nil {
errorRes["error_description"] = err.Error()
c.JSON(400, errorRes)
return
}
2022-03-02 12:12:31 +00:00
user, err := db.Provider.GetUserByEmail(claim["sub"].(string))
if err != nil {
errorRes["error_description"] = err.Error()
c.JSON(400, errorRes)
return
}
// update email_verified_at in users table
if user.EmailVerifiedAt == nil {
now := time.Now().Unix()
user.EmailVerifiedAt = &now
2022-01-21 08:04:04 +00:00
db.Provider.UpdateUser(user)
}
// delete from verification table
2022-01-21 08:04:04 +00:00
db.Provider.DeleteVerificationRequest(verificationRequest)
state := strings.TrimSpace(c.Query("state"))
redirectURL := strings.TrimSpace(c.Query("redirect_uri"))
rolesString := strings.TrimSpace(c.Query("roles"))
var roles []string
if rolesString == "" {
roles = strings.Split(user.Roles, ",")
} else {
roles = strings.Split(rolesString, ",")
}
scopeString := strings.TrimSpace(c.Query("scope"))
var scope []string
if scopeString == "" {
scope = []string{"openid", "email", "profile"}
} else {
scope = strings.Split(scopeString, " ")
}
authToken, err := token.CreateAuthToken(c, user, roles, scope)
if err != nil {
errorRes["error_description"] = err.Error()
c.JSON(500, errorRes)
return
}
expiresIn := authToken.AccessToken.ExpiresAt - time.Now().Unix()
if expiresIn <= 0 {
expiresIn = 1
}
params := "access_token=" + authToken.AccessToken.Token + "&token_type=bearer&expires_in=" + strconv.FormatInt(expiresIn, 10) + "&state=" + state + "&id_token=" + authToken.IDToken.Token
cookie.SetSession(c, authToken.FingerPrintHash)
sessionstore.SetState(authToken.FingerPrintHash, authToken.FingerPrint+"@"+user.ID)
sessionstore.SetState(authToken.AccessToken.Token, authToken.FingerPrint+"@"+user.ID)
if authToken.RefreshToken != nil {
params = params + `&refresh_token=${refresh_token}`
2022-03-08 15:43:23 +00:00
sessionstore.SetState(authToken.RefreshToken.Token, authToken.FingerPrint+"@"+user.ID)
}
if redirectURL == "" {
2022-03-08 17:25:45 +00:00
redirectURL = claim["redirect_uri"].(string)
}
if strings.Contains(redirectURL, "?") {
redirectURL = redirectURL + "&" + params
} else {
redirectURL = redirectURL + "?" + params
}
2022-03-02 12:12:31 +00:00
go utils.SaveSessionInDB(c, user.ID)
c.Redirect(http.StatusTemporaryRedirect, redirectURL)
}
}