forked from digitalocean/atc
/
list_auth_methods.go
99 lines (79 loc) · 2.25 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
package authserver
import (
"encoding/json"
"errors"
"net/http"
"sort"
"github.com/concourse/atc"
"github.com/concourse/atc/auth/provider"
"github.com/concourse/atc/dbng"
"github.com/concourse/atc/web"
"github.com/tedsuo/rata"
)
const BasicAuthDisplayName = "Basic Auth"
func (s *Server) ListAuthMethods(w http.ResponseWriter, r *http.Request) {
logger := s.logger.Session("list-auth-methods")
teamName := r.FormValue(":team_name")
team, found, err := s.teamFactory.FindTeam(teamName)
if err != nil {
logger.Error("failed-to-get-team", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
logger.Info("team-not-found")
w.WriteHeader(http.StatusNotFound)
return
}
methods, err := s.authMethods(team)
if err != nil {
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 dbng.Team) ([]atc.AuthMethod, error) {
methods := []atc.AuthMethod{}
providers := provider.GetProviders()
for providerName, config := range team.Auth() {
p, found := providers[providerName]
if !found {
return nil, errors.New("failed to find provider")
}
authConfig, err := p.UnmarshalConfig(config)
if err != nil {
return nil, err
}
methods = append(methods, authConfig.AuthMethod(s.oAuthBaseURL, team.Name()))
}
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
}