forked from rogpeppe-contrib/candid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
202 lines (179 loc) · 5.71 KB
/
test.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// Package test contains an identity provider useful for testing other
// parts of the system. The test identity provider is insecure by design
// so should not be used in any production system.
package test
import (
"net/http"
"strings"
"golang.org/x/net/context"
"gopkg.in/CanonicalLtd/candidclient.v1/params"
"gopkg.in/errgo.v1"
"gopkg.in/httprequest.v1"
"gopkg.in/macaroon-bakery.v2/httpbakery"
"github.com/CanonicalLtd/candid/idp"
"github.com/CanonicalLtd/candid/idp/idputil"
"github.com/CanonicalLtd/candid/store"
)
func init() {
idp.Register("test", func(unmarshal func(interface{}) error) (idp.IdentityProvider, error) {
var p Params
if err := unmarshal(&p); err != nil {
return nil, errgo.Mask(err)
}
if p.Name == "" {
p.Name = "test"
}
return NewIdentityProvider(p), nil
})
}
type Params struct {
// Name is the name that will be used with the identity provider.
Name string
// Domain contains the domain that will be used with the identity
// provider.
Domain string
// GetGroups contains function that if set will be called by
// GetGroups to obtain the groups to return.
GetGroups func(*store.Identity) ([]string, error)
}
// NewIdentityProvider creates an idp.IdentityProvider that can be used
// for tests.
func NewIdentityProvider(p Params) idp.IdentityProvider {
return &identityProvider{
params: p,
}
}
type identityProvider struct {
params Params
initParams idp.InitParams
}
// Name implements idp.IdentityProvider.Name.
func (idp *identityProvider) Name() string {
return idp.params.Name
}
// Domain implements idp.IdentityProvider.Domain.
func (idp *identityProvider) Domain() string {
return idp.params.Domain
}
// Description gives a description of the identity provider.
func (*identityProvider) Description() string {
return "Test"
}
// Interactive specifies that this identity provider is interactive.
func (*identityProvider) Interactive() bool {
return true
}
// Init implements idp.IdentityProvider.Init.
func (idp *identityProvider) Init(ctx context.Context, params idp.InitParams) error {
idp.initParams = params
return nil
}
// URL gets the login URL to use this identity provider.
func (idp *identityProvider) URL(dischargeID string) string {
return idputil.URL(idp.initParams.URLPrefix, "/login", dischargeID)
}
// SetInteraction sets the interaction information for this identity provider.
func (idp *identityProvider) SetInteraction(ierr *httpbakery.Error, dischargeID string) {
ierr.SetInteraction(authType, authInfo{
URL: idputil.URL(idp.initParams.URLPrefix, "/interact", dischargeID),
})
}
// GetGroups implements idp.IdentityProvider.GetGroups.
func (idp *identityProvider) GetGroups(_ context.Context, id *store.Identity) ([]string, error) {
f := idp.params.GetGroups
if f == nil {
return nil, nil
}
return f(id)
}
// Handle handles the login process.
func (idp *identityProvider) Handle(ctx context.Context, w http.ResponseWriter, req *http.Request) {
id, err := idp.handle(ctx, w, req)
if err != nil {
idp.initParams.VisitCompleter.Failure(ctx, w, req, idputil.DischargeID(req), err)
} else if id != nil {
idp.initParams.VisitCompleter.Success(ctx, w, req, idputil.DischargeID(req), id)
}
}
type testInteractiveLoginResponse struct {
URL string `json:"url"`
}
func (idp *identityProvider) handle(ctx context.Context, w http.ResponseWriter, req *http.Request) (*store.Identity, error) {
switch req.Method {
case "GET":
httprequest.WriteJSON(w, http.StatusOK, testInteractiveLoginResponse{
URL: idp.URL(idputil.DischargeID(req)),
})
case "POST":
return idp.handlePost(ctx, w, req)
default:
return nil, errgo.WithCausef(nil, params.ErrMethodNotAllowed, "%s not allowed", req.Method)
}
return nil, nil
}
func (idp *identityProvider) handlePost(ctx context.Context, w http.ResponseWriter, req *http.Request) (*store.Identity, error) {
var lr testLoginRequest
if err := httprequest.Unmarshal(idputil.RequestParams(ctx, w, req), &lr); err != nil {
return nil, err
}
if lr.User.ExternalID != "" && lr.User.Username != "" {
if err := idp.updateUser(ctx, lr.User); err != nil {
return nil, errgo.Mask(err, errgo.Is(params.ErrAlreadyExists))
}
}
id := store.Identity{
ProviderID: store.ProviderIdentity(lr.User.ExternalID),
Username: string(lr.User.Username),
}
if err := idp.initParams.Store.Identity(ctx, &id); err != nil {
if errgo.Cause(err) == store.ErrNotFound {
return nil, errgo.WithCausef(err, params.ErrNotFound, "")
}
return nil, errgo.Mask(err)
}
switch strings.TrimPrefix(req.URL.Path, idp.initParams.URLPrefix) {
default:
return nil, errgo.WithCausef(nil, params.ErrNotFound, "path %q not found", req.URL.Path)
case "/interact":
dt, err := idp.initParams.DischargeTokenCreator.DischargeToken(ctx, &id)
if err != nil {
return nil, errgo.Mask(err)
}
httprequest.WriteJSON(w, http.StatusOK, testTokenResponse{
DischargeToken: dt,
})
return nil, nil
case "/login":
return &id, nil
}
}
func (idp *identityProvider) updateUser(ctx context.Context, u *params.User) error {
id := store.Identity{
ProviderID: store.ProviderIdentity(u.ExternalID),
Username: string(u.Username),
Name: u.FullName,
Email: u.Email,
Groups: u.IDPGroups,
}
update := store.Update{
store.Username: store.Set,
}
if id.Name != "" {
update[store.Name] = store.Set
}
if id.Email != "" {
update[store.Email] = store.Set
}
if len(id.Groups) > 0 {
update[store.Groups] = store.Set
}
if err := idp.initParams.Store.UpdateIdentity(ctx, &id, update); err != nil {
if errgo.Cause(err) == store.ErrDuplicateUsername {
return errgo.WithCausef(err, params.ErrAlreadyExists, "")
}
return errgo.Mask(err)
}
return nil
}