|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/base64" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/authorizerdev/authorizer/server/cookie" |
| 10 | + "github.com/authorizerdev/authorizer/server/db" |
| 11 | + "github.com/authorizerdev/authorizer/server/sessionstore" |
| 12 | + "github.com/authorizerdev/authorizer/server/token" |
| 13 | + "github.com/gin-gonic/gin" |
| 14 | +) |
| 15 | + |
| 16 | +func TokenHandler() gin.HandlerFunc { |
| 17 | + return func(gc *gin.Context) { |
| 18 | + var reqBody map[string]string |
| 19 | + if err := gc.BindJSON(&reqBody); err != nil { |
| 20 | + gc.JSON(http.StatusBadRequest, gin.H{ |
| 21 | + "error": "error_binding_json", |
| 22 | + "error_description": err.Error(), |
| 23 | + }) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + codeVerifier := strings.TrimSpace(reqBody["code_verifier"]) |
| 28 | + code := strings.TrimSpace(reqBody["code"]) |
| 29 | + redirectURI := strings.TrimSpace(reqBody["redirect_uri"]) |
| 30 | + |
| 31 | + if codeVerifier == "" { |
| 32 | + gc.JSON(http.StatusBadRequest, gin.H{ |
| 33 | + "error": "invalid_code_verifier", |
| 34 | + "error_description": "The code verifier is required", |
| 35 | + }) |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + if code == "" { |
| 40 | + gc.JSON(http.StatusBadRequest, gin.H{ |
| 41 | + "error": "invalid_code", |
| 42 | + "error_description": "The code is required", |
| 43 | + }) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + if redirectURI == "" { |
| 48 | + gc.JSON(http.StatusBadRequest, gin.H{ |
| 49 | + "error": "invalid_redirect_uri", |
| 50 | + "error_description": "The redirect URI is required", |
| 51 | + }) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + hash := sha256.New() |
| 56 | + hash.Write([]byte(codeVerifier)) |
| 57 | + encryptedCode := strings.TrimSuffix(base64.URLEncoding.EncodeToString(hash.Sum(nil)), "=") |
| 58 | + sessionData := sessionstore.GetState(encryptedCode) |
| 59 | + if sessionData == "" { |
| 60 | + gc.JSON(http.StatusBadRequest, gin.H{ |
| 61 | + "error": "invalid_code_verifier", |
| 62 | + "error_description": "The code verifier is invalid", |
| 63 | + }) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + // split session data |
| 68 | + // it contains code@sessiontoken |
| 69 | + sessionDataSplit := strings.Split(sessionData, "@") |
| 70 | + |
| 71 | + if sessionDataSplit[0] != code { |
| 72 | + gc.JSON(http.StatusBadRequest, gin.H{ |
| 73 | + "error": "invalid_code_verifier", |
| 74 | + "error_description": "The code verifier is invalid", |
| 75 | + }) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + // validate session |
| 80 | + claims, err := token.ValidateBrowserSession(gc, sessionDataSplit[1]) |
| 81 | + if err != nil { |
| 82 | + gc.JSON(http.StatusUnauthorized, gin.H{ |
| 83 | + "error": "unauthorized", |
| 84 | + "error_description": "Invalid session data", |
| 85 | + }) |
| 86 | + return |
| 87 | + } |
| 88 | + userID := claims.Subject |
| 89 | + user, err := db.Provider.GetUserByID(userID) |
| 90 | + if err != nil { |
| 91 | + gc.JSON(http.StatusUnauthorized, gin.H{ |
| 92 | + "error": "unauthorized", |
| 93 | + "error_description": "User not found", |
| 94 | + }) |
| 95 | + return |
| 96 | + } |
| 97 | + // rollover the session for security |
| 98 | + sessionstore.RemoveState(sessionDataSplit[1]) |
| 99 | + authToken, err := token.CreateAuthToken(gc, user, claims.Roles, claims.Scope) |
| 100 | + if err != nil { |
| 101 | + gc.JSON(http.StatusUnauthorized, gin.H{ |
| 102 | + "error": "unauthorized", |
| 103 | + "error_description": "User not found", |
| 104 | + }) |
| 105 | + return |
| 106 | + } |
| 107 | + sessionstore.SetState(authToken.FingerPrintHash, authToken.FingerPrint+"@"+user.ID) |
| 108 | + sessionstore.SetState(authToken.AccessToken.Token, authToken.FingerPrint+"@"+user.ID) |
| 109 | + cookie.SetSession(gc, authToken.FingerPrintHash) |
| 110 | + |
| 111 | + expiresIn := int64(1800) |
| 112 | + res := map[string]interface{}{ |
| 113 | + "access_token": authToken.AccessToken.Token, |
| 114 | + "id_token": authToken.IDToken.Token, |
| 115 | + "scope": claims.Scope, |
| 116 | + "expires_in": expiresIn, |
| 117 | + } |
| 118 | + |
| 119 | + if authToken.RefreshToken != nil { |
| 120 | + res["refresh_token"] = authToken.RefreshToken.Token |
| 121 | + sessionstore.SetState(authToken.AccessToken.Token, authToken.FingerPrint+"@"+user.ID) |
| 122 | + } |
| 123 | + |
| 124 | + gc.JSON(http.StatusOK, res) |
| 125 | + } |
| 126 | +} |
0 commit comments