-
Notifications
You must be signed in to change notification settings - Fork 4
/
keystone.go
169 lines (143 loc) · 4.43 KB
/
keystone.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
package api
import (
"net/http"
"time"
"github.com/emicklei/go-restful"
"github.com/mcluseau/autorizo/auth"
)
// KeystoneAuthReq is a Keystone API like auth request
type KeystoneAuthReq struct {
Auth *KeystoneAuth `json:"auth"`
}
// KeystoneAuth is a Keystone API like auth
type KeystoneAuth struct {
Identity struct {
Methods []string `json:"methods"`
Password struct {
User struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Password string `json:"password"`
Domain struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
} `json:"domain"`
} `json:"user"`
} `json:"password"`
} `json:"identity"`
}
// KeystoneAuthResponse is a Keystone API like auth response
type KeystoneAuthResponse struct {
Token struct {
IssuedAt time.Time `json:"issued_at"`
ExpiresAt time.Time `json:"expires_at"`
User struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
} `json:"user"`
} `json:"token"`
}
func (api *API) registerKeystone(ws *restful.WebService) {
path := "/v3/auth/tokens"
ws.
Route(ws.POST(path).
To(api.keystoneAuthenticate).
Doc("Authenticate using a Keystone-style request").
Consumes("application/json").
Produces("application/json").
Reads(KeystoneAuthReq{}).
Writes(KeystoneAuthResponse{}))
ws.
Route(ws.GET(path).
To(api.keystoneShow).
Doc("Validates and shows information for a token").
Produces("application/json").
Param(restful.HeaderParameter(
"X-Auth-Token", "A valid authentication token for an administrative user.")).
Param(restful.HeaderParameter(
"X-Subject-Token", "The authentication token.")).
Writes(KeystoneAuthResponse{}))
ws.
Route(ws.HEAD(path).
To(api.keystoneCheck).
Doc("Validates a token").
Param(restful.HeaderParameter(
"X-Auth-Token", "A valid authentication token for an administrative user.")).
Param(restful.HeaderParameter(
"X-Subject-Token", "The authentication token.")))
}
func (api *API) keystoneAuthenticate(request *restful.Request, response *restful.Response) {
defer func() {
if err := recover(); err != nil {
// unhandled error
WriteError(err.(error), response)
}
}()
authReq := KeystoneAuthReq{}
if err := request.ReadEntity(&authReq); err != nil {
WriteError(err, response)
return
}
if authReq.Auth == nil {
response.WriteErrorString(http.StatusUnauthorized, "No authentication provided")
return
}
user := authReq.Auth.Identity.Password.User
login := user.ID
if login == "" {
login = user.Name
}
claims, err := api.authenticate(login, user.Password)
if err == ErrInvalidAuthentication {
response.WriteErrorString(http.StatusUnauthorized, "Authentication failed")
return
} else if err != nil {
panic(err)
}
_, tokenString, err := api.createToken(login, claims)
if err != nil {
panic(err)
}
stdClaims, err := api.checkToken(tokenString)
if err != nil {
panic(err)
}
authResp := newKeystoneAuthRespFromClaims(stdClaims)
response.Header().Set("X-Subject-Token", tokenString)
response.WriteHeaderAndEntity(http.StatusCreated, authResp)
}
func newKeystoneAuthRespFromClaims(claims *auth.Claims) *KeystoneAuthResponse {
authResp := &KeystoneAuthResponse{}
authResp.Token.IssuedAt = time.Unix(claims.IssuedAt, 0)
authResp.Token.ExpiresAt = time.Unix(claims.ExpiresAt, 0)
authResp.Token.User.ID = claims.Subject
authResp.Token.User.Name = claims.Subject
return authResp
}
func (api *API) keystoneCheck(request *restful.Request, response *restful.Response) {
api.keystoneCheckClaims(request, response)
response.Header().Set("Content-Length", "0")
response.WriteHeader(http.StatusOK)
}
func (api *API) keystoneShow(request *restful.Request, response *restful.Response) {
claims := api.keystoneCheckClaims(request, response)
if claims == nil {
return
}
response.WriteHeaderAndEntity(http.StatusOK, newKeystoneAuthRespFromClaims(claims))
}
// return nil iff check fails (response already filled)
func (api *API) keystoneCheckClaims(request *restful.Request, response *restful.Response) *auth.Claims {
authToken := request.HeaderParameter("X-Auth-Token")
if _, err := api.checkToken(authToken); err != nil {
response.WriteError(http.StatusUnauthorized, err)
return nil
}
subjectToken := request.HeaderParameter("X-Subject-Token")
claims, err := api.checkToken(subjectToken)
if err != nil {
response.WriteError(http.StatusBadRequest, err)
return nil
}
return claims
}