@@ -24,21 +24,22 @@ import (
2424// It verifies email based on JWT token in query string
2525func VerifyEmailHandler () gin.HandlerFunc {
2626 return func (c * gin.Context ) {
27+ redirectURL := strings .TrimSpace (c .Query ("redirect_uri" ))
2728 errorRes := gin.H {
28- "error" : "invalid_token " ,
29+ "error" : "token is required " ,
2930 }
3031 tokenInQuery := c .Query ("token" )
3132 if tokenInQuery == "" {
3233 log .Debug ("Token is empty" )
33- c . JSON ( 400 , errorRes )
34+ utils . HandleRedirectORJsonResponse ( c , http . StatusBadRequest , errorRes , generateRedirectURL ( redirectURL , errorRes ) )
3435 return
3536 }
3637
3738 verificationRequest , err := db .Provider .GetVerificationRequestByToken (c , tokenInQuery )
3839 if err != nil {
3940 log .Debug ("Error getting verification request: " , err )
40- errorRes ["error_description " ] = err .Error ()
41- c . JSON ( 400 , errorRes )
41+ errorRes ["error " ] = err .Error ()
42+ utils . HandleRedirectORJsonResponse ( c , http . StatusBadRequest , errorRes , generateRedirectURL ( redirectURL , errorRes ) )
4243 return
4344 }
4445
@@ -47,23 +48,23 @@ func VerifyEmailHandler() gin.HandlerFunc {
4748 claim , err := token .ParseJWTToken (tokenInQuery )
4849 if err != nil {
4950 log .Debug ("Error parsing token: " , err )
50- errorRes ["error_description " ] = err .Error ()
51- c . JSON ( 400 , errorRes )
51+ errorRes ["error " ] = err .Error ()
52+ utils . HandleRedirectORJsonResponse ( c , http . StatusBadRequest , errorRes , generateRedirectURL ( redirectURL , errorRes ) )
5253 return
5354 }
5455
5556 if ok , err := token .ValidateJWTClaims (claim , hostname , verificationRequest .Nonce , verificationRequest .Email ); ! ok || err != nil {
5657 log .Debug ("Error validating jwt claims: " , err )
57- errorRes ["error_description " ] = err .Error ()
58- c . JSON ( 400 , errorRes )
58+ errorRes ["error " ] = err .Error ()
59+ utils . HandleRedirectORJsonResponse ( c , http . StatusBadRequest , errorRes , generateRedirectURL ( redirectURL , errorRes ) )
5960 return
6061 }
6162
6263 user , err := db .Provider .GetUserByEmail (c , verificationRequest .Email )
6364 if err != nil {
6465 log .Debug ("Error getting user: " , err )
65- errorRes ["error_description " ] = err .Error ()
66- c . JSON ( 400 , errorRes )
66+ errorRes ["error " ] = err .Error ()
67+ utils . HandleRedirectORJsonResponse ( c , http . StatusBadRequest , errorRes , generateRedirectURL ( redirectURL , errorRes ) )
6768 return
6869 }
6970
@@ -79,7 +80,6 @@ func VerifyEmailHandler() gin.HandlerFunc {
7980 db .Provider .DeleteVerificationRequest (c , verificationRequest )
8081
8182 state := strings .TrimSpace (c .Query ("state" ))
82- redirectURL := strings .TrimSpace (c .Query ("redirect_uri" ))
8383 rolesString := strings .TrimSpace (c .Query ("roles" ))
8484 var roles []string
8585 if rolesString == "" {
@@ -125,8 +125,8 @@ func VerifyEmailHandler() gin.HandlerFunc {
125125 authToken , err := token .CreateAuthToken (c , user , roles , scope , loginMethod , nonce , code )
126126 if err != nil {
127127 log .Debug ("Error creating auth token: " , err )
128- errorRes ["error_description " ] = err .Error ()
129- c . JSON ( 500 , errorRes )
128+ errorRes ["error " ] = err .Error ()
129+ utils . HandleRedirectORJsonResponse ( c , http . StatusInternalServerError , errorRes , generateRedirectURL ( redirectURL , errorRes ) )
130130 return
131131 }
132132
@@ -135,7 +135,7 @@ func VerifyEmailHandler() gin.HandlerFunc {
135135 // if code != "" {
136136 // if err := memorystore.Provider.SetState(code, codeChallenge+"@@"+authToken.FingerPrintHash); err != nil {
137137 // log.Debug("Error setting code state ", err)
138- // errorRes["error_description "] = err.Error()
138+ // errorRes["error "] = err.Error()
139139 // c.JSON(500, errorRes)
140140 // return
141141 // }
@@ -189,3 +189,21 @@ func VerifyEmailHandler() gin.HandlerFunc {
189189 c .Redirect (http .StatusTemporaryRedirect , redirectURL )
190190 }
191191}
192+
193+ func generateRedirectURL (url string , res map [string ]interface {}) string {
194+ redirectURL := url
195+ if redirectURL == "" {
196+ return ""
197+ }
198+ var paramsArr []string
199+ for key , value := range res {
200+ paramsArr = append (paramsArr , key + "=" + value .(string ))
201+ }
202+ params := strings .Join (paramsArr , "&" )
203+ if strings .Contains (redirectURL , "?" ) {
204+ redirectURL = redirectURL + "&" + params
205+ } else {
206+ redirectURL = redirectURL + "?" + strings .TrimPrefix (params , "&" )
207+ }
208+ return redirectURL
209+ }
0 commit comments