forked from vmware-archive/atc
/
provider.go
92 lines (76 loc) · 2.08 KB
/
provider.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
83
84
85
86
87
88
89
90
91
92
package github
import (
"net/http"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc/auth/verifier"
"github.com/concourse/atc/db"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
)
const ProviderName = "github"
const DisplayName = "GitHub"
var Scopes = []string{"read:org"}
type Provider interface {
PreTokenClient() (*http.Client, error)
OAuthClient
Verifier
}
type OAuthClient interface {
AuthCodeURL(string, ...oauth2.AuthCodeOption) string
Exchange(context.Context, string) (*oauth2.Token, error)
Client(context.Context, *oauth2.Token) *http.Client
}
type Verifier interface {
Verify(lager.Logger, *http.Client) (bool, error)
}
func NewProvider(
gitHubAuth *db.GitHubAuth,
redirectURL string,
) Provider {
client := NewClient(gitHubAuth.APIURL)
endpoint := github.Endpoint
if gitHubAuth.AuthURL != "" && gitHubAuth.TokenURL != "" {
endpoint.AuthURL = gitHubAuth.AuthURL
endpoint.TokenURL = gitHubAuth.TokenURL
}
return gitHubProvider{
Verifier: verifier.NewVerifierBasket(
NewTeamVerifier(dbTeamsToGitHubTeams(gitHubAuth.Teams), client),
NewOrganizationVerifier(gitHubAuth.Organizations, client),
NewUserVerifier(gitHubAuth.Users, client),
),
Config: &oauth2.Config{
ClientID: gitHubAuth.ClientID,
ClientSecret: gitHubAuth.ClientSecret,
Endpoint: endpoint,
Scopes: Scopes,
RedirectURL: redirectURL,
},
}
}
type gitHubProvider struct {
*oauth2.Config
// oauth2.Config implements the required Provider methods:
// AuthCodeURL(string, ...oauth2.AuthCodeOption) string
// Exchange(context.Context, string) (*oauth2.Token, error)
// Client(context.Context, *oauth2.Token) *http.Client
verifier.Verifier
}
func dbTeamsToGitHubTeams(dbteams []db.GitHubTeam) []Team {
teams := []Team{}
for _, team := range dbteams {
teams = append(teams, Team{
Name: team.TeamName,
Organization: team.OrganizationName,
})
}
return teams
}
func (gitHubProvider) PreTokenClient() (*http.Client, error) {
return &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
}, nil
}