forked from vmware-archive/atc
/
list_auth_methods.go
136 lines (115 loc) · 3.21 KB
/
list_auth_methods.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package authserver
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"github.com/concourse/atc"
"github.com/concourse/atc/auth"
"github.com/concourse/atc/auth/genericoauth"
"github.com/concourse/atc/auth/github"
"github.com/concourse/atc/auth/uaa"
"github.com/concourse/atc/db"
"github.com/concourse/atc/web"
"github.com/tedsuo/rata"
)
const BasicAuthDisplayName = "Basic Auth"
func (s *Server) ListAuthMethods(w http.ResponseWriter, r *http.Request) {
teamName := r.FormValue(":team_name")
teamDB := s.teamDBFactory.GetTeamDB(teamName)
team, found, err := teamDB.GetTeam()
if err != nil {
s.logger.Error("failed-to-get-team", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
s.logger.Info("team-not-found")
w.WriteHeader(http.StatusNotFound)
return
}
methods, err := s.authMethods(team)
if err != nil {
s.logger.Error("failed-to-get-auth-methods", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
sort.Sort(byTypeAndName(methods))
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(methods)
}
type byTypeAndName []atc.AuthMethod
func (ms byTypeAndName) Len() int { return len(ms) }
func (ms byTypeAndName) Swap(i int, j int) { ms[i], ms[j] = ms[j], ms[i] }
func (ms byTypeAndName) Less(i int, j int) bool {
if ms[i].Type == atc.AuthTypeBasic && ms[j].Type == atc.AuthTypeOAuth {
return false
}
if ms[i].Type == atc.AuthTypeOAuth && ms[j].Type == atc.AuthTypeBasic {
return true
}
return ms[i].DisplayName < ms[j].DisplayName
}
func (s *Server) authMethods(team db.SavedTeam) ([]atc.AuthMethod, error) {
methods := []atc.AuthMethod{}
if team.GitHubAuth != nil {
path, err := auth.OAuthRoutes.CreatePathForRoute(
auth.OAuthBegin,
rata.Params{"provider": github.ProviderName},
)
if err != nil {
return nil, err
}
path = path + fmt.Sprintf("?team_name=%s", team.Name)
methods = append(methods, atc.AuthMethod{
Type: atc.AuthTypeOAuth,
DisplayName: github.DisplayName,
AuthURL: s.oAuthBaseURL + path,
})
}
if team.UAAAuth != nil {
path, err := auth.OAuthRoutes.CreatePathForRoute(
auth.OAuthBegin,
rata.Params{"provider": uaa.ProviderName},
)
if err != nil {
return nil, err
}
path = path + fmt.Sprintf("?team_name=%s", team.Name)
methods = append(methods, atc.AuthMethod{
Type: atc.AuthTypeOAuth,
DisplayName: uaa.DisplayName,
AuthURL: s.oAuthBaseURL + path,
})
}
if team.GenericOAuth != nil {
path, err := auth.OAuthRoutes.CreatePathForRoute(
auth.OAuthBegin,
rata.Params{"provider": genericoauth.ProviderName},
)
if err != nil {
return nil, err
}
path = path + fmt.Sprintf("?team_name=%s", team.Name)
methods = append(methods, atc.AuthMethod{
Type: atc.AuthTypeOAuth,
DisplayName: team.GenericOAuth.DisplayName,
AuthURL: s.oAuthBaseURL + path,
})
}
if team.BasicAuth != nil {
path, err := web.Routes.CreatePathForRoute(
web.TeamLogIn,
rata.Params{"team_name": team.Name},
)
if err != nil {
return nil, err
}
methods = append(methods, atc.AuthMethod{
Type: atc.AuthTypeBasic,
DisplayName: BasicAuthDisplayName,
AuthURL: s.externalURL + path,
})
}
return methods, nil
}