-
Notifications
You must be signed in to change notification settings - Fork 29
/
login.go
179 lines (160 loc) · 4.98 KB
/
login.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package debug
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"github.com/juju/usso"
"github.com/juju/usso/openid"
"github.com/julienschmidt/httprouter"
"golang.org/x/crypto/nacl/box"
"gopkg.in/CanonicalLtd/candidclient.v1/params"
"gopkg.in/errgo.v1"
"gopkg.in/macaroon-bakery.v2/bakery"
"github.com/CanonicalLtd/candid/internal/identity"
)
const cookieName = "debug-login"
// loginRequiredError is an error that indicates that a login request
// should be attempted.
type loginRequiredError struct {
redirectURL string
}
func (*loginRequiredError) ErrorCode() params.ErrorCode {
return identity.ErrLoginRequired
}
// Error implements error.Error.
func (err *loginRequiredError) Error() string {
return fmt.Sprintf("login required to %q", err.redirectURL)
}
// SetHeader implements httprequest.HeaderSetter.
func (err *loginRequiredError) SetHeader(h http.Header) {
h.Set("Location", err.redirectURL)
}
// cookie contains the data stored in the debug login cookie.
type cookie struct {
// ExpireTime contains the time after which the cookie is
// invalid.
ExpireTime time.Time
// ID contains the Ubuntu SSO ID of the user.
ID string
// Teams contains the subset of params.DebugTeams of which the
// user is a member.
Teams []string
}
func encodeCookie(k *bakery.KeyPair, c *cookie) (string, error) {
data, err := json.Marshal(c)
if err != nil {
return "", errgo.Mask(err)
}
var nonce [24]byte
_, err = rand.Read(nonce[:])
if err != nil {
return "", errgo.Mask(err)
}
edata := nonce[:]
edata = box.Seal(edata, data, &nonce, (*[bakery.KeyLen]byte)(&k.Public.Key), (*[bakery.KeyLen]byte)(&k.Private.Key))
return base64.StdEncoding.EncodeToString(edata), nil
}
func decodeCookie(k *bakery.KeyPair, v string) (*cookie, error) {
edata, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return nil, errgo.Notef(err, "cannot decode cookie")
}
var nonce [24]byte
n := copy(nonce[:], edata)
edata = edata[n:]
data, ok := box.Open(nil, edata, &nonce, (*[bakery.KeyLen]byte)(&k.Public.Key), (*[bakery.KeyLen]byte)(&k.Private.Key))
if !ok {
return nil, errgo.New("cannot decrypt cookie")
}
var cookie cookie
if err := json.Unmarshal(data, &cookie); err != nil {
return nil, errgo.Notef(err, "cannot unmarshal cookie")
}
return &cookie, nil
}
// checkLogin checks that the given request contains a valid login cookie
// for accessing /debug endpoints. If there is any sort of error decoding
// the cookie the returned error will have a cause of type
// *loginRequiredError.
func (h *debugAPIHandler) checkLogin(r *http.Request) error {
c, err := r.Cookie(cookieName)
if err != nil {
return errgo.WithCausef(err, h.loginRequired(r), "no cookie")
}
cookie, err := decodeCookie(h.key, c.Value)
if err != nil {
return errgo.WithCausef(nil, h.loginRequired(r), "%s", err.Error())
}
if cookie.ExpireTime.Before(time.Now()) {
return errgo.WithCausef(nil, h.loginRequired(r), "cookie expired")
}
for _, t1 := range cookie.Teams {
for _, t2 := range h.teams {
if t1 == t2 {
return nil
}
}
}
return errgo.WithCausef(nil, h.loginRequired(r), "no suitable team membership")
}
// ussoClient is the client for the Ubuntu SSO server.
var ussoClient = openid.NewClient(usso.ProductionUbuntuSSOServer, nil, nil)
// loginRequired creates a new loginRequiredError for the given request.
func (h *debugAPIHandler) loginRequired(r *http.Request) *loginRequiredError {
return &loginRequiredError{
redirectURL: ussoClient.RedirectURL(&openid.Request{
ReturnTo: h.location + "/debug/login?return_to=" + url.QueryEscape(h.location+r.URL.String()),
Realm: h.location + "/debug",
Teams: h.teams,
}),
}
}
// login handles callbacks from an Ubuntu SSO login attempt.
func (h *debugAPIHandler) login(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
url := h.location + r.URL.String()
resp, err := ussoClient.Verify(url)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "unauthorized: %s\n", err)
return
}
for _, t1 := range resp.Teams {
for _, t2 := range h.teams {
if t1 == t2 {
h.loginSuccess(w, r, resp)
return
}
}
}
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "unauthorized: access denied for %s", resp.ID)
}
// loginSuccess completes a login when it has been deemed successful.
func (h *debugAPIHandler) loginSuccess(w http.ResponseWriter, r *http.Request, resp *openid.Response) {
c := &cookie{
ExpireTime: time.Now().Add(1 * time.Hour),
ID: resp.ID,
Teams: resp.Teams,
}
value, err := encodeCookie(h.key, c)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "cannot create cookie: %s", err)
return
}
http.SetCookie(w, &http.Cookie{
Name: cookieName,
Value: value,
Path: "/debug",
Expires: c.ExpireTime,
})
r.ParseForm()
w.Header().Set("Location", r.Form.Get("return_to"))
w.WriteHeader(http.StatusSeeOther)
}