This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
common.go
82 lines (60 loc) · 1.79 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package handlers
import (
"fmt"
"net/url"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
)
// OpenFaaSCloudClaims extends standard claims
type OpenFaaSCloudClaims struct {
// Name is the full name of the user for OIDC
Name string `json:"name"`
// AccessToken for use with the GitHub Profile API
AccessToken string `json:"access_token"`
// String with all organizations separated with commas
Organizations string `json:"organizations"`
// Inherit from standard claims
jwt.StandardClaims
}
// ProviderAccessToken as issued by GitHub or GitLab
type ProviderAccessToken struct {
AccessToken string `json:"access_token"`
}
func buildGitHubURL(config *Config, resource string, scope string) *url.URL {
authURL := "https://github.com/login/oauth/authorize"
u, _ := url.Parse(authURL)
q := u.Query()
q.Set("scope", scope)
q.Set("allow_signup", "0")
q.Set("state", fmt.Sprintf("%d", time.Now().Unix()))
q.Set("client_id", config.ClientID)
redirectURI := combineURL(config.ExternalRedirectDomain, "/oauth2/authorized")
q.Set("redirect_uri", redirectURI)
u.RawQuery = q.Encode()
return u
}
func buildGitLabURL(config *Config) *url.URL {
authURL := config.OAuthProviderBaseURL + "/oauth/authorize"
u, _ := url.Parse(authURL)
q := u.Query()
q.Set("client_id", config.ClientID)
q.Set("response_type", "code")
q.Set("state", fmt.Sprintf("%d", time.Now().Unix()))
redirectURI := combineURL(config.ExternalRedirectDomain, "/oauth2/authorized")
q.Set("redirect_uri", redirectURI)
u.RawQuery = q.Encode()
return u
}
func combineURL(a, b string) string {
if !strings.HasSuffix(a, "/") {
a = a + "/"
}
if strings.HasPrefix(b, "/") {
b = strings.TrimPrefix(b, "/")
}
return a + b
}
func (c *OpenFaaSCloudClaims) GetOrganizations() []string {
return strings.Split(c.Organizations, ",")
}