forked from vmware-archive/atc
/
oauth_factory.go
61 lines (49 loc) · 1.4 KB
/
oauth_factory.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
package provider
import (
"errors"
"github.com/cloudfoundry/gunk/urljoiner"
"github.com/concourse/atc/auth/github"
"github.com/concourse/atc/db"
"github.com/tedsuo/rata"
)
//go:generate counterfeiter . FactoryDB
type FactoryDB interface {
GetTeamByName(teamName string) (db.SavedTeam, bool, error)
}
type OAuthFactory struct {
db FactoryDB
atcExternalURL string
routes rata.Routes
callback string
}
func NewOAuthFactory(db FactoryDB, atcExternalURL string, routes rata.Routes, callback string) OAuthFactory {
return OAuthFactory{
db: db,
atcExternalURL: atcExternalURL,
routes: routes,
callback: callback,
}
}
func (of OAuthFactory) GetProviders(teamName string) (Providers, error) {
team, found, err := of.db.GetTeamByName(teamName)
if err != nil {
return Providers{}, err
}
if !found {
return Providers{}, errors.New("team not found")
}
providers := Providers{}
if len(team.GitHubAuth.Organizations) > 0 ||
len(team.GitHubAuth.Teams) > 0 ||
len(team.GitHubAuth.Users) > 0 {
redirectURL, err := of.routes.CreatePathForRoute(of.callback, rata.Params{
"provider": github.ProviderName,
})
if err != nil {
return Providers{}, err
}
gitHubAuthProvider := github.NewProvider(team.GitHubAuth, urljoiner.Join(of.atcExternalURL, redirectURL))
providers[github.ProviderName] = gitHubAuthProvider
}
return providers, err
}