Skip to content

Commit 648b17c

Browse files
committed
adds gitlab
1 parent 2bcd26e commit 648b17c

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

recipe/thirdparty/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ func Bitbucket(config tpmodels.BitbucketConfig) tpmodels.TypeProvider {
102102
return providers.Bitbucket(config)
103103
}
104104

105+
func GitLab(config tpmodels.GitLabConfig) tpmodels.TypeProvider {
106+
return providers.GitLab(config)
107+
}
108+
105109
func Google(config tpmodels.GoogleConfig) tpmodels.TypeProvider {
106110
return providers.Google(config)
107111
}

recipe/thirdparty/providers/bitbucket.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func Bitbucket(config tpmodels.BitbucketConfig) tpmodels.TypeProvider {
111111
ID: ID,
112112
}, nil
113113
}
114+
114115
return tpmodels.UserInfo{
115116
ID: ID,
116117
Email: &tpmodels.EmailStruct{
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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 gitlabID = "gitlab"
28+
29+
func GitLab(config tpmodels.GitLabConfig) tpmodels.TypeProvider {
30+
gitLabURL := "https://gitlab.com"
31+
if config.GitLabBaseURL != nil {
32+
url, err := supertokens.NewNormalisedURLDomain(*config.GitLabBaseURL)
33+
if err != nil {
34+
panic(err)
35+
}
36+
gitLabURL = url.GetAsStringDangerous()
37+
}
38+
return tpmodels.TypeProvider{
39+
ID: gitlabID,
40+
Get: func(redirectURI, authCodeFromRequest *string, userContext supertokens.UserContext) tpmodels.TypeProviderGetResponse {
41+
accessTokenAPIURL := gitLabURL + "/oauth/token"
42+
accessTokenAPIParams := map[string]string{
43+
"client_id": config.ClientID,
44+
"client_secret": config.ClientSecret,
45+
"grant_type": "authorization_code",
46+
}
47+
if authCodeFromRequest != nil {
48+
accessTokenAPIParams["code"] = *authCodeFromRequest
49+
}
50+
if redirectURI != nil {
51+
accessTokenAPIParams["redirect_uri"] = *redirectURI
52+
}
53+
54+
authorisationRedirectURL := gitLabURL + "/oauth/authorize"
55+
scopes := []string{"read_user"}
56+
if config.Scope != nil {
57+
scopes = config.Scope
58+
}
59+
60+
var additionalParams map[string]interface{} = nil
61+
if config.AuthorisationRedirect != nil && config.AuthorisationRedirect.Params != nil {
62+
additionalParams = config.AuthorisationRedirect.Params
63+
}
64+
65+
authorizationRedirectParams := map[string]interface{}{
66+
"scope": strings.Join(scopes, " "),
67+
"response_type": "code",
68+
"client_id": config.ClientID,
69+
}
70+
for key, value := range additionalParams {
71+
authorizationRedirectParams[key] = value
72+
}
73+
74+
return tpmodels.TypeProviderGetResponse{
75+
AccessTokenAPI: tpmodels.AccessTokenAPI{
76+
URL: accessTokenAPIURL,
77+
Params: accessTokenAPIParams,
78+
},
79+
AuthorisationRedirect: tpmodels.AuthorisationRedirect{
80+
URL: authorisationRedirectURL,
81+
Params: authorizationRedirectParams,
82+
},
83+
GetProfileInfo: func(authCodeResponse interface{}, userContext supertokens.UserContext) (tpmodels.UserInfo, error) {
84+
authCodeResponseJson, err := json.Marshal(authCodeResponse)
85+
if err != nil {
86+
return tpmodels.UserInfo{}, err
87+
}
88+
var accessTokenAPIResponse gitlabGetProfileInfoInput
89+
err = json.Unmarshal(authCodeResponseJson, &accessTokenAPIResponse)
90+
if err != nil {
91+
return tpmodels.UserInfo{}, err
92+
}
93+
accessToken := accessTokenAPIResponse.AccessToken
94+
authHeader := "Bearer " + accessToken
95+
response, err := getGitLabAuthRequest(gitLabURL, authHeader)
96+
if err != nil {
97+
return tpmodels.UserInfo{}, err
98+
}
99+
userInfo := response.(map[string]interface{})
100+
ID := userInfo["id"].(string)
101+
_, emailExists := userInfo["email"]
102+
if !emailExists {
103+
return tpmodels.UserInfo{
104+
ID: ID,
105+
}, nil
106+
}
107+
email := userInfo["email"].(string)
108+
var isVerified bool
109+
_, ok := userInfo["confirmed_at"]
110+
if ok && userInfo["confirmed_at"] != nil && userInfo["confirmed_at"].(string) != "" {
111+
isVerified = true
112+
} else {
113+
isVerified = false
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 getGitLabAuthRequest(gitLabUrl string, authHeader string) (interface{}, error) {
133+
url := gitLabUrl + "/api/v4/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+
type gitlabGetProfileInfoInput struct {
143+
AccessToken string `json:"access_token"`
144+
}

recipe/thirdparty/tpmodels/providers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ type BitbucketConfig struct {
3535
IsDefault bool
3636
}
3737

38+
type GitLabConfig struct {
39+
ClientID string
40+
ClientSecret string
41+
Scope []string
42+
AuthorisationRedirect *struct {
43+
Params map[string]interface{}
44+
}
45+
GitLabBaseURL *string
46+
IsDefault bool
47+
}
48+
3849
type GoogleWorkspacesConfig struct {
3950
ClientID string
4051
ClientSecret string

0 commit comments

Comments
 (0)