Merge lp:~martin-hilton/usso/get-token-with-2fa into lp:usso

Proposed by Martin Hilton
Status: Needs review
Proposed branch: lp:~martin-hilton/usso/get-token-with-2fa
Merge into: lp:usso
Diff against target: 84 lines (+51/-0)
2 files modified
usso.go (+11/-0)
usso_test.go (+40/-0)
To merge this branch: bzr merge lp:~martin-hilton/usso/get-token-with-2fa
Reviewer Review Type Date Requested Status
Matthew Williams (community) Approve
Casey Marshall Approve
Uros Jovanovic (community) Approve
Review via email: mp+265479@code.launchpad.net

Description of the change

Add a method which will retrieve a token using two-factor authentication in addition to email and password.

To post a comment you must log in.
Revision history for this message
Uros Jovanovic (uros-jovanovic) wrote :

LGTM

review: Approve
Revision history for this message
Matthew Williams (mattyw) wrote :

LGTM

review: Approve
Revision history for this message
Matthew Williams (mattyw) :
Revision history for this message
Casey Marshall (cmars) :
review: Approve
Revision history for this message
Matthew Williams (mattyw) :
review: Needs Fixing
46. By Martin Hilton on 2015-07-23

review comment

Revision history for this message
Matthew Williams (mattyw) :
review: Approve

Unmerged revisions

46. By Martin Hilton on 2015-07-23

review comment

45. By Martin Hilton on 2015-07-23

review comment

44. By Martin Hilton on 2015-07-22

support retrieving tokens using two-factor authentication

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'usso.go'
2--- usso.go 2014-02-27 16:30:55 +0000
3+++ usso.go 2015-07-23 07:27:43 +0000
4@@ -46,11 +46,22 @@
5 // Giving user credentials and token name, retrieves oauth credentials
6 // for the users, the oauth credentials can be used later to sign requests.
7 func (server UbuntuSSOServer) GetToken(email string, password string, tokenName string) (*SSOData, error) {
8+ return server.GetTokenWithOTP(email, password, "", tokenName)
9+}
10+
11+// GetTokenWithOTP retrieves an oauth token from the Ubuntu SSO server.
12+// Using the user credentials including two-factor authentication and the
13+// token name, an oauth token is retrieved that can later be used to sign
14+// requests. If otp is blank then this is identical to GetToken.
15+func (server UbuntuSSOServer) GetTokenWithOTP(email, password, otp, tokenName string) (*SSOData, error) {
16 credentials := map[string]string{
17 "email": email,
18 "password": password,
19 "token_name": tokenName,
20 }
21+ if otp != "" {
22+ credentials["otp"] = otp
23+ }
24 jsonCredentials, err := json.Marshal(credentials)
25 if err != nil {
26 return nil, err
27
28=== modified file 'usso_test.go'
29--- usso_test.go 2014-02-27 17:18:28 +0000
30+++ usso_test.go 2015-07-23 07:27:43 +0000
31@@ -26,6 +26,7 @@
32 consumerSecret = "rwDkQkkdfdfdeAslkmmxAOjOAT"
33 email = "foo@bar.com"
34 password = "foobarpwd"
35+ otp = "000000"
36 )
37
38 // TestProductionUbuntuSSOServerURLs tests the URLs of the production server.
39@@ -160,6 +161,45 @@
40 c.Assert(token_details, Equals, string(jsonTokenDetails))
41 }
42
43+func (suite *USSOTestSuite) TestGetTokenWithOTP(c *C) {
44+ // Simulate a valid Ubuntu SSO Server response.
45+ serverResponseData := map[string]string{
46+ "date_updated": "2013-01-16 14:03:36",
47+ "date_created": "2013-01-16 14:03:36",
48+ "href": "/api/v2/tokens/" + tokenKey,
49+ "token_name": tokenName,
50+ "token_key": tokenKey,
51+ "token_secret": tokenSecret,
52+ "consumer_key": consumerKey,
53+ "consumer_secret": consumerSecret,
54+ }
55+ jsonServerResponseData, err := json.Marshal(serverResponseData)
56+ if err != nil {
57+ panic(err)
58+ }
59+ server := newTestServer(string(jsonServerResponseData), "{}", 200)
60+ var testSSOServer = &UbuntuSSOServer{server.URL, ""}
61+ defer server.Close()
62+
63+ // The returned information is correct.
64+ ssodata, err := testSSOServer.GetTokenWithOTP(email, password, otp, tokenName)
65+ c.Assert(err, IsNil)
66+ expectedSSOData := &SSOData{ConsumerKey: consumerKey,
67+ ConsumerSecret: consumerSecret, Realm: realm, TokenKey: tokenKey,
68+ TokenSecret: tokenSecret, TokenName: tokenName}
69+ c.Assert(ssodata, DeepEquals, expectedSSOData)
70+ // The request that the fake Ubuntu SSO Server has the credentials.
71+ credentials := map[string]string{
72+ "email": email,
73+ "password": password,
74+ "token_name": tokenName,
75+ "otp": otp,
76+ }
77+ expectedRequestContent, err := json.Marshal(credentials)
78+ c.Assert(err, IsNil)
79+ c.Assert(*server.requestContent, Equals, string(expectedRequestContent))
80+}
81+
82 func (suite *USSOTestSuite) TestTokenValidity(c *C) {
83 // Simulate a valid Ubuntu SSO Server response.
84 serverResponseData := map[string]string{

Subscribers

People subscribed via source and target branches