forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration_test.go
195 lines (172 loc) · 6 KB
/
registration_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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver_test
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
jc "github.com/juju/testing/checkers"
"github.com/juju/testing/httptesting"
"github.com/juju/utils"
"golang.org/x/crypto/nacl/secretbox"
gc "gopkg.in/check.v1"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
)
type registrationSuite struct {
authHTTPSuite
bob *state.User
}
var _ = gc.Suite(®istrationSuite{})
func (s *registrationSuite) SetUpTest(c *gc.C) {
s.authHTTPSuite.SetUpTest(c)
bob, err := s.BackingState.AddUserWithSecretKey("bob", "", "admin")
c.Assert(err, jc.ErrorIsNil)
s.bob = bob
}
func (s *registrationSuite) registrationURL(c *gc.C) string {
url := s.baseURL(c)
url.Path = "/register"
return url.String()
}
func (s *registrationSuite) TestRegister(c *gc.C) {
// Ensure we cannot log in with the password yet.
const password = "hunter2"
c.Assert(s.bob.PasswordValid(password), jc.IsFalse)
validNonce := []byte(strings.Repeat("X", 24))
secretKey := s.bob.SecretKey()
ciphertext := s.sealBox(
c, validNonce, secretKey, fmt.Sprintf(`{"password": "%s"}`, password),
)
resp := httptesting.Do(c, httptesting.DoRequestParams{
Do: utils.GetNonValidatingHTTPClient().Do,
URL: s.registrationURL(c),
Method: "POST",
JSONBody: ¶ms.SecretKeyLoginRequest{
User: "user-bob",
Nonce: validNonce,
PayloadCiphertext: ciphertext,
},
})
c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
defer resp.Body.Close()
// It should be possible to log in as bob with the
// password "hunter2" now, and there should be no
// secret key any longer.
err := s.bob.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(s.bob.PasswordValid(password), jc.IsTrue)
c.Assert(s.bob.SecretKey(), gc.IsNil)
var response params.SecretKeyLoginResponse
bodyData, err := ioutil.ReadAll(resp.Body)
c.Assert(err, jc.ErrorIsNil)
err = json.Unmarshal(bodyData, &response)
c.Assert(err, jc.ErrorIsNil)
c.Assert(response.Nonce, gc.HasLen, len(validNonce))
plaintext := s.openBox(c, response.PayloadCiphertext, response.Nonce, secretKey)
var responsePayload params.SecretKeyLoginResponsePayload
err = json.Unmarshal(plaintext, &responsePayload)
c.Assert(err, jc.ErrorIsNil)
c.Assert(responsePayload.CACert, gc.Equals, s.BackingState.CACert())
model, err := s.BackingState.Model()
c.Assert(err, jc.ErrorIsNil)
c.Assert(responsePayload.ControllerUUID, gc.Equals, model.ControllerUUID())
}
func (s *registrationSuite) TestRegisterInvalidMethod(c *gc.C) {
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
Do: utils.GetNonValidatingHTTPClient().Do,
URL: s.registrationURL(c),
Method: "GET",
ExpectStatus: http.StatusMethodNotAllowed,
ExpectBody: ¶ms.ErrorResult{
Error: ¶ms.Error{
Message: `unsupported method: "GET"`,
Code: params.CodeMethodNotAllowed,
},
},
})
}
func (s *registrationSuite) TestRegisterInvalidFormat(c *gc.C) {
s.testInvalidRequest(
c, "[]", "json: cannot unmarshal array into Go value of type params.SecretKeyLoginRequest", "",
http.StatusInternalServerError,
)
}
func (s *registrationSuite) TestRegisterInvalidUserTag(c *gc.C) {
s.testInvalidRequest(
c, `{"user": "application-bob"}`, `"application-bob" is not a valid user tag`, "",
http.StatusInternalServerError,
)
}
func (s *registrationSuite) TestRegisterInvalidNonce(c *gc.C) {
s.testInvalidRequest(
c, `{"user": "user-bob", "nonce": ""}`, `nonce not valid`, "",
http.StatusInternalServerError,
)
}
func (s *registrationSuite) TestRegisterInvalidCiphertext(c *gc.C) {
validNonce := []byte(strings.Repeat("X", 24))
s.testInvalidRequest(c,
fmt.Sprintf(
`{"user": "user-bob", "nonce": "%s"}`,
base64.StdEncoding.EncodeToString(validNonce),
), `secret key not valid`, "",
http.StatusInternalServerError,
)
}
func (s *registrationSuite) TestRegisterNoSecretKey(c *gc.C) {
err := s.bob.SetPassword("anything")
c.Assert(err, jc.ErrorIsNil)
validNonce := []byte(strings.Repeat("X", 24))
s.testInvalidRequest(c,
fmt.Sprintf(
`{"user": "user-bob", "nonce": "%s"}`,
base64.StdEncoding.EncodeToString(validNonce),
), `secret key for user "bob" not found`, params.CodeNotFound,
http.StatusNotFound,
)
}
func (s *registrationSuite) TestRegisterInvalidRequestPayload(c *gc.C) {
validNonce := []byte(strings.Repeat("X", 24))
ciphertext := s.sealBox(c, validNonce, s.bob.SecretKey(), "[]")
s.testInvalidRequest(c,
fmt.Sprintf(
`{"user": "user-bob", "nonce": "%s", "cipher-text": "%s"}`,
base64.StdEncoding.EncodeToString(validNonce),
base64.StdEncoding.EncodeToString(ciphertext),
),
`cannot unmarshal payload: json: cannot unmarshal array into Go value of type params.SecretKeyLoginRequestPayload`, "",
http.StatusInternalServerError,
)
}
func (s *registrationSuite) testInvalidRequest(c *gc.C, requestBody, errorMessage, errorCode string, statusCode int) {
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
Do: utils.GetNonValidatingHTTPClient().Do,
URL: s.registrationURL(c),
Method: "POST",
Body: strings.NewReader(requestBody),
ExpectStatus: statusCode,
ExpectBody: ¶ms.ErrorResult{
Error: ¶ms.Error{Message: errorMessage, Code: errorCode},
},
})
}
func (s *registrationSuite) sealBox(c *gc.C, nonce, key []byte, message string) []byte {
var nonceArray [24]byte
var keyArray [32]byte
c.Assert(copy(nonceArray[:], nonce), gc.Equals, len(nonceArray))
c.Assert(copy(keyArray[:], key), gc.Equals, len(keyArray))
return secretbox.Seal(nil, []byte(message), &nonceArray, &keyArray)
}
func (s *registrationSuite) openBox(c *gc.C, ciphertext, nonce, key []byte) []byte {
var nonceArray [24]byte
var keyArray [32]byte
c.Assert(copy(nonceArray[:], nonce), gc.Equals, len(nonceArray))
c.Assert(copy(keyArray[:], key), gc.Equals, len(keyArray))
message, ok := secretbox.Open(nil, ciphertext, &nonceArray, &keyArray)
c.Assert(ok, jc.IsTrue)
return message
}