Merge lp:~vds/usso/kind_of_login into lp:usso

Proposed by Vincenzo Di Somma
Status: Merged
Approved by: Casey Marshall
Approved revision: 30
Merged at revision: 30
Proposed branch: lp:~vds/usso/kind_of_login
Merge into: lp:usso
Diff against target: 125 lines (+67/-5)
3 files modified
example/usso_example.go (+2/-3)
usso.go (+36/-1)
usso_test.go (+29/-1)
To merge this branch: bzr merge lp:~vds/usso/kind_of_login
Reviewer Review Type Date Requested Status
Casey Marshall (community) Approve
Review via email: mp+193462@code.launchpad.net

Commit message

Adding support for getting token details.

Description of the change

Adding support for getting token details.

To post a comment you must log in.
Revision history for this message
Casey Marshall (cmars) wrote :

I updated my usso_test app to evaluate the contents of the token details, and it seems to work well with the following test.

Using https://bazaar.launchpad.net/~cmars/+junk/usso_test/view/head:/main.go, I signed into usso, authorizing the test application token, "test-usso-token". The resulting auth string was valid when checked against USSO, token details were thus:

{
    "token_name": "test-usso-token",
    "date_updated": "2013-10-31T19:28:50.268",
    "token_key": "[redacted]",
    "href": "/api/v2/tokens/oauth/[redacted, same as token_key's value]",
    "date_created": "2013-10-31T19:28:50.268",
    "consumer_key": "[redacted]"
}

Then I went to the Applications tab on login.ubuntu.com and test-usso-token was listed as an authorized application. I deleted the authorization there, and a subsequent USSO query with the auth string came back with token details:

{"message": "Provided email/password is not correct.", "code": "INVALID_CREDENTIALS", "extra": {}}

This looks very good to me!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'example/usso_example.go'
2--- example/usso_example.go 2013-02-05 12:37:03 +0000
3+++ example/usso_example.go 2013-10-31 17:37:25 +0000
4@@ -23,12 +23,9 @@
5
6 func main() {
7 inputParams()
8-
9 // Fetch the tokens using usso.GetToken.
10 fmt.Println("Fetching tokens from staging server...")
11 server := usso.StagingUbuntuSSOServer
12- // One would use server := usso.ProductionUbuntuSSOServer
13- // to use the production Ubuntu SSO Server.
14 ssodata, err := server.GetToken(email, password, "usso")
15 if err != nil {
16 panic(err)
17@@ -67,4 +64,6 @@
18 var b bytes.Buffer
19 b.Write(body)
20 fmt.Printf("response: %+v\n", b.String())
21+ token_details, _ := server.GetTokenDetails(ssodata)
22+ fmt.Printf("token details: %s\n", token_details)
23 }
24
25=== modified file 'usso.go'
26--- usso.go 2013-02-27 15:14:29 +0000
27+++ usso.go 2013-10-31 17:37:25 +0000
28@@ -20,12 +20,18 @@
29 return server.baseUrl + "/api/v2/tokens/oauth"
30 }
31
32-// AccountURL returns the URL where the Ubuntu SSO account information can be
33+// AccountURL returns the URL where the Ubuntu SSO account information can be
34 // requested.
35 func (server UbuntuSSOServer) AccountsURL() string {
36 return server.baseUrl + "/api/v2/accounts/"
37 }
38
39+// TokenDetailURL returns the URL where the Ubuntu SSO token details can be
40+// requested.
41+func (server UbuntuSSOServer) TokenDetailsURL() string {
42+ return server.baseUrl + "/api/v2/tokens/oauth/"
43+}
44+
45 // ProductionUbuntuSSOServer represents the production Ubuntu SSO server
46 // located at https://login.ubuntu.com.
47 var ProductionUbuntuSSOServer = UbuntuSSOServer{"https://login.ubuntu.com"}
48@@ -116,3 +122,32 @@
49 header, err := ssodata.GetAuthorizationHeader(rp)
50 return header, err
51 }
52+
53+// Returns all the Ubuntu SSO information related to this token.
54+func (server UbuntuSSOServer) GetTokenDetails(ssodata *SSOData) (string, error) {
55+ rp := RequestParameters{
56+ BaseURL: server.TokenDetailsURL() + ssodata.TokenKey,
57+ HTTPMethod: "GET",
58+ SignatureMethod: HMACSHA1{}}
59+
60+ request, err := http.NewRequest(rp.HTTPMethod, rp.BaseURL, nil)
61+ if err != nil {
62+ return "", err
63+ }
64+ err = SignRequest(ssodata, &rp, request)
65+ if err != nil {
66+ return "", err
67+ }
68+ client := &http.Client{}
69+ response, err := client.Do(request)
70+ if err != nil {
71+ fmt.Printf("Error: %s\n", err)
72+ }
73+ body, err := ioutil.ReadAll(response.Body)
74+ if err != nil {
75+ fmt.Println(err)
76+ }
77+ var b bytes.Buffer
78+ b.Write(body)
79+ return fmt.Sprint(b.String()), nil
80+}
81
82=== modified file 'usso_test.go'
83--- usso_test.go 2013-02-27 15:14:29 +0000
84+++ usso_test.go 2013-10-31 17:37:25 +0000
85@@ -99,7 +99,7 @@
86 ConsumerSecret: consumerSecret, TokenKey: tokenKey,
87 TokenSecret: tokenSecret, TokenName: tokenName}
88 c.Assert(ssodata, DeepEquals, expectedSSOData)
89- //The request that the fake Ubuntu SSO Server got contained the credentials.
90+ // The request that the fake Ubuntu SSO Server has the credentials.
91 credentials := map[string]string{
92 "email": email,
93 "password": password,
94@@ -122,3 +122,31 @@
95 c.Assert(err, NotNil)
96 c.Assert(ssodata, IsNil)
97 }
98+
99+func (suite *USSOTestSuite) TestGetTokenDetails(c *C) {
100+ // Simulate a valid Ubuntu SSO Server response.
101+ serverResponseData := map[string]string{
102+ "date_updated": "2013-01-16 14:03:36",
103+ "date_created": "2013-01-16 14:03:36",
104+ "href": "/api/v2/tokens/" + tokenKey,
105+ "token_name": tokenName,
106+ "token_key": tokenKey,
107+ "consumer_key": consumerKey,
108+ }
109+ jsonServerResponseData, err := json.Marshal(serverResponseData)
110+ if err != nil {
111+ panic(err)
112+ }
113+ server := newSingleServingServer("/api/v2/tokens/oauth",
114+ string(jsonServerResponseData), 200)
115+ var testSSOServer = &UbuntuSSOServer{server.URL}
116+ defer server.Close()
117+ ssodata, err := testSSOServer.GetToken(email, password, tokenName)
118+
119+ // The returned information is correct.
120+ token_details, err := testSSOServer.GetTokenDetails(ssodata)
121+ c.Assert(err, IsNil)
122+
123+ //The request that the fake Ubuntu SSO Server has the token details.
124+ fmt.Printf("%s\n", token_details)
125+}

Subscribers

People subscribed via source and target branches

to all changes: