|
| 1 | +/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved. |
| 2 | + * |
| 3 | + * This software is licensed under the Apache License, Version 2.0 (the |
| 4 | + * "License") as published by the Apache Software Foundation. |
| 5 | + * |
| 6 | + * You may not use this file except in compliance with the License. You may |
| 7 | + * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | + * License for the specific language governing permissions and limitations |
| 13 | + * under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package providers |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "net/http" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels" |
| 24 | + "github.com/supertokens/supertokens-golang/supertokens" |
| 25 | +) |
| 26 | + |
| 27 | +const bitbucketID = "bitbucket" |
| 28 | + |
| 29 | +func Bitbucket(config tpmodels.BitbucketConfig) tpmodels.TypeProvider { |
| 30 | + return tpmodels.TypeProvider{ |
| 31 | + ID: bitbucketID, |
| 32 | + Get: func(redirectURI, authCodeFromRequest *string, userContext supertokens.UserContext) tpmodels.TypeProviderGetResponse { |
| 33 | + accessTokenAPIURL := "https://bitbucket.org/site/oauth2/access_token" |
| 34 | + accessTokenAPIParams := map[string]string{ |
| 35 | + "client_id": config.ClientID, |
| 36 | + "client_secret": config.ClientSecret, |
| 37 | + "grant_type": "authorization_code", |
| 38 | + } |
| 39 | + if authCodeFromRequest != nil { |
| 40 | + accessTokenAPIParams["code"] = *authCodeFromRequest |
| 41 | + } |
| 42 | + if redirectURI != nil { |
| 43 | + accessTokenAPIParams["redirect_uri"] = *redirectURI |
| 44 | + } |
| 45 | + |
| 46 | + authorisationRedirectURL := "https://bitbucket.org/site/oauth2/authorize" |
| 47 | + scopes := []string{"account", "email"} |
| 48 | + if config.Scope != nil { |
| 49 | + scopes = config.Scope |
| 50 | + } |
| 51 | + |
| 52 | + var additionalParams map[string]interface{} = nil |
| 53 | + if config.AuthorisationRedirect != nil && config.AuthorisationRedirect.Params != nil { |
| 54 | + additionalParams = config.AuthorisationRedirect.Params |
| 55 | + } |
| 56 | + |
| 57 | + authorizationRedirectParams := map[string]interface{}{ |
| 58 | + "scope": strings.Join(scopes, " "), |
| 59 | + "access_type": "offline", |
| 60 | + "response_type": "code", |
| 61 | + "client_id": config.ClientID, |
| 62 | + } |
| 63 | + for key, value := range additionalParams { |
| 64 | + authorizationRedirectParams[key] = value |
| 65 | + } |
| 66 | + |
| 67 | + return tpmodels.TypeProviderGetResponse{ |
| 68 | + AccessTokenAPI: tpmodels.AccessTokenAPI{ |
| 69 | + URL: accessTokenAPIURL, |
| 70 | + Params: accessTokenAPIParams, |
| 71 | + }, |
| 72 | + AuthorisationRedirect: tpmodels.AuthorisationRedirect{ |
| 73 | + URL: authorisationRedirectURL, |
| 74 | + Params: authorizationRedirectParams, |
| 75 | + }, |
| 76 | + GetProfileInfo: func(authCodeResponse interface{}, userContext supertokens.UserContext) (tpmodels.UserInfo, error) { |
| 77 | + authCodeResponseJson, err := json.Marshal(authCodeResponse) |
| 78 | + if err != nil { |
| 79 | + return tpmodels.UserInfo{}, err |
| 80 | + } |
| 81 | + var accessTokenAPIResponse bitbucketGetProfileInfoInput |
| 82 | + err = json.Unmarshal(authCodeResponseJson, &accessTokenAPIResponse) |
| 83 | + if err != nil { |
| 84 | + return tpmodels.UserInfo{}, err |
| 85 | + } |
| 86 | + accessToken := accessTokenAPIResponse.AccessToken |
| 87 | + authHeader := "Bearer " + accessToken |
| 88 | + response, err := getBitbucketAuthRequest(authHeader) |
| 89 | + if err != nil { |
| 90 | + return tpmodels.UserInfo{}, err |
| 91 | + } |
| 92 | + userInfo := response.(map[string]interface{}) |
| 93 | + ID := userInfo["uuid"].(string) |
| 94 | + |
| 95 | + emailResponse, err := getBitbucketEmailRequest(authHeader) |
| 96 | + if err != nil { |
| 97 | + return tpmodels.UserInfo{}, err |
| 98 | + } |
| 99 | + var email string |
| 100 | + var isVerified bool = false |
| 101 | + emailResponseInfo := emailResponse.(map[string]interface{}) |
| 102 | + for _, emailInfo := range emailResponseInfo["values"].([]interface{}) { |
| 103 | + emailInfoMap := emailInfo.(map[string]interface{}) |
| 104 | + if emailInfoMap["is_primary"].(bool) { |
| 105 | + email = emailInfoMap["email"].(string) |
| 106 | + isVerified = emailInfoMap["is_confirmed"].(bool) |
| 107 | + } |
| 108 | + } |
| 109 | + if email == "" { |
| 110 | + return tpmodels.UserInfo{ |
| 111 | + ID: ID, |
| 112 | + }, nil |
| 113 | + } |
| 114 | + |
| 115 | + return tpmodels.UserInfo{ |
| 116 | + ID: ID, |
| 117 | + Email: &tpmodels.EmailStruct{ |
| 118 | + ID: email, |
| 119 | + IsVerified: isVerified, |
| 120 | + }, |
| 121 | + }, nil |
| 122 | + }, |
| 123 | + GetClientId: func(userContext supertokens.UserContext) string { |
| 124 | + return config.ClientID |
| 125 | + }, |
| 126 | + } |
| 127 | + }, |
| 128 | + IsDefault: config.IsDefault, |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func getBitbucketAuthRequest(authHeader string) (interface{}, error) { |
| 133 | + url := "https://api.bitbucket.org/2.0/user" |
| 134 | + req, err := http.NewRequest("GET", url, nil) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + req.Header.Add("Authorization", authHeader) |
| 139 | + return doGetRequest(req) |
| 140 | +} |
| 141 | + |
| 142 | +func getBitbucketEmailRequest(authHeader string) (interface{}, error) { |
| 143 | + url := "https://api.bitbucket.org/2.0/user/emails" |
| 144 | + req, err := http.NewRequest("GET", url, nil) |
| 145 | + if err != nil { |
| 146 | + return nil, err |
| 147 | + } |
| 148 | + req.Header.Add("Authorization", authHeader) |
| 149 | + return doGetRequest(req) |
| 150 | +} |
| 151 | + |
| 152 | +type bitbucketGetProfileInfoInput struct { |
| 153 | + AccessToken string `json:"access_token"` |
| 154 | +} |
0 commit comments