Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add Resource Owner to response

  • Loading branch information...
commit f76ec6e1f308b333016f54001db5c65b91a50863 1 parent e431cff
Bip Thelin bipthelin authored
6 src/oauth2.erl
@@ -266,7 +266,7 @@ issue_code(Identity, Scope, ResOwner, TTL) ->
266 266 ExpiryAbsolute = seconds_since_epoch(TTL),
267 267 Context = build_context(Identity, ExpiryAbsolute, ResOwner, Scope),
268 268 oauth2_backend:associate_access_code(AccessCode, Context),
269   - oauth2_response:new([], TTL, Scope, [], AccessCode).
  269 + oauth2_response:new([], TTL, ResOwner, Scope, [], AccessCode).
270 270
271 271 -spec issue_token_and_refresh(Identity, ResOwner, Scope, TTL) -> oauth2_response:response() when
272 272 Identity :: term(),
@@ -279,7 +279,7 @@ issue_token_and_refresh(Identity, ResOwner, Scope, TTL) ->
279 279 ExpiryAbsolute = seconds_since_epoch(TTL),
280 280 Context = build_context(Identity, ExpiryAbsolute, ResOwner, Scope),
281 281 oauth2_backend:associate_access_token(AccessToken, Context),
282   - oauth2_response:new(AccessToken, TTL, Scope, RefreshToken).
  282 + oauth2_response:new(AccessToken, TTL, ResOwner, Scope, RefreshToken).
283 283
284 284 -spec issue_token(Identity, ResOwner, Scope, TTL) -> oauth2_response:response() when
285 285 Identity :: term(),
@@ -291,7 +291,7 @@ issue_token(Identity, ResOwner, Scope, TTL) ->
291 291 ExpiryAbsolute = seconds_since_epoch(TTL),
292 292 Context = build_context(Identity, ExpiryAbsolute, ResOwner, Scope),
293 293 oauth2_backend:associate_access_token(AccessToken, Context),
294   - oauth2_response:new(AccessToken, TTL, Scope).
  294 + oauth2_response:new(AccessToken, TTL, ResOwner, Scope).
295 295
296 296 -spec build_context(Identity, ExpiryTime, ResOwner, Scope) -> Context when
297 297 Identity :: term(),
30 src/oauth2_response.erl
@@ -33,15 +33,17 @@
33 33 -export([
34 34 new/1
35 35 ,new/2
36   - ,new/3
37 36 ,new/4
38 37 ,new/5
  38 + ,new/6
39 39 ,access_token/1
40 40 ,access_token/2
41 41 ,access_code/1
42 42 ,access_code/2
43 43 ,refresh_token/1
44 44 ,refresh_token/2
  45 + ,resource_owner/1
  46 + ,resource_owner/2
45 47 ,expires_in/1
46 48 ,expires_in/2
47 49 ,scope/1
@@ -50,11 +52,12 @@
50 52 ]).
51 53
52 54 -record(response, {
53   - access_token :: oauth2:token()
54   - ,access_code :: oauth2:token()
55   - ,expires_in :: oauth2:lifetime()
56   - ,scope :: oauth2:scope()
57   - ,refresh_token :: oauth2:token()
  55 + access_token :: oauth2:token()
  56 + ,access_code :: oauth2:token()
  57 + ,expires_in :: oauth2:lifetime()
  58 + ,resource_owner :: term()
  59 + ,scope :: oauth2:scope()
  60 + ,refresh_token :: oauth2:token()
58 61 ,token_type = <<"bearer">> :: binary()
59 62 }).
60 63
@@ -73,20 +76,23 @@ new(AccessToken) ->
73 76 new(AccessToken, ExpiresIn) ->
74 77 #response{access_token = AccessToken, expires_in = ExpiresIn}.
75 78
76   -new(AccessToken, ExpiresIn, Scope) ->
  79 +new(AccessToken, ExpiresIn, ResOwner, Scope) ->
77 80 #response{access_token = AccessToken,
78 81 expires_in = ExpiresIn,
  82 + resource_owner = ResOwner,
79 83 scope = Scope}.
80 84
81   -new(AccessToken, ExpiresIn, Scope, RefreshToken) ->
  85 +new(AccessToken, ExpiresIn, ResOwner, Scope, RefreshToken) ->
82 86 #response{access_token = AccessToken,
83 87 expires_in = ExpiresIn,
  88 + resource_owner = ResOwner,
84 89 scope = Scope,
85 90 refresh_token = RefreshToken}.
86 91
87   -new(_, ExpiresIn, Scope, _, AccessCode) ->
  92 +new(_, ExpiresIn, ResOwner, Scope, _, AccessCode) ->
88 93 #response{access_code = AccessCode,
89 94 expires_in = ExpiresIn,
  95 + resource_owner = ResOwner,
90 96 scope = Scope}.
91 97
92 98 access_token(#response{access_token = undefined}) ->
@@ -127,6 +133,12 @@ refresh_token(#response{refresh_token = RefreshToken}) ->
127 133 refresh_token(Response, NewRefreshToken) ->
128 134 Response#response{refresh_token = NewRefreshToken}.
129 135
  136 +resource_owner(#response{resource_owner = ResOwner}) ->
  137 + {ok, ResOwner}.
  138 +
  139 +resource_owner(Response, NewResOwner) ->
  140 + Response#response{resource_owner = NewResOwner}.
  141 +
130 142 to_proplist(Response) ->
131 143 Keys = lists:map(fun to_binary/1, record_info(fields, response)),
132 144 Values = tl(tuple_to_list(Response)), %% Head is 'response'!
23 test/oauth2_response_tests.erl
@@ -31,6 +31,7 @@
31 31 -define(ACCESS, <<"9bX9iFUOsXbM12OOjfDW175IXXOELp6K">>).
32 32 -define(REFRESH, <<"JVs3ZFQJBIdduJdhhWOoAt2B3qEKcHEo">>).
33 33 -define(CODE, <<"Lz7Z24cKSQ28z8kem01ZP9c0aE3TEbGl">>).
  34 +-define(RESOURCE_OWNER, <<"user">>).
34 35 -define(EXPIRY, 3600).
35 36 -define(SCOPE, <<"herp derp">>).
36 37
@@ -64,9 +65,9 @@ new_2_test_() ->
64 65 ]
65 66 end}.
66 67
67   -new_3_test_() ->
  68 +new_4_test_() ->
68 69 {setup,
69   - fun() -> oauth2_response:new(?ACCESS, ?EXPIRY, ?SCOPE) end,
  70 + fun() -> oauth2_response:new(?ACCESS, ?EXPIRY, ?RESOURCE_OWNER, ?SCOPE) end,
70 71 fun(_) -> ok end,
71 72 fun(Response) ->
72 73 [
@@ -77,9 +78,9 @@ new_3_test_() ->
77 78 ]
78 79 end}.
79 80
80   -new_4_test_() ->
  81 +new_5_test_() ->
81 82 {setup,
82   - fun() -> oauth2_response:new(?ACCESS, ?EXPIRY, ?SCOPE, ?REFRESH) end,
  83 + fun() -> oauth2_response:new(?ACCESS, ?EXPIRY, ?RESOURCE_OWNER, ?SCOPE, ?REFRESH) end,
83 84 fun(_) -> ok end,
84 85 fun(Response) ->
85 86 [
@@ -91,9 +92,9 @@ new_4_test_() ->
91 92 end}.
92 93
93 94
94   -new_5_test_() ->
  95 +new_6_test_() ->
95 96 {setup,
96   - fun() -> oauth2_response:new(?ACCESS, ?EXPIRY, ?SCOPE, ?REFRESH, ?CODE) end,
  97 + fun() -> oauth2_response:new(?ACCESS, ?EXPIRY, ?RESOURCE_OWNER, ?SCOPE, ?REFRESH, ?CODE) end,
97 98 fun(_) -> ok end,
98 99 fun(Response) ->
99 100 [
@@ -141,10 +142,18 @@ refresh_token_test() ->
141 142 oauth2_response:new(?ACCESS),
142 143 ?REFRESH))).
143 144
  145 +resource_owner_test() ->
  146 + ?assertEqual({ok, ?RESOURCE_OWNER},
  147 + oauth2_response:resource_owner(
  148 + oauth2_response:resource_owner(
  149 + oauth2_response:new(?ACCESS),
  150 + ?RESOURCE_OWNER))).
  151 +
144 152 to_proplist_test() ->
145   - Response = oauth2_response:new(?ACCESS, ?EXPIRY, ?SCOPE, ?REFRESH),
  153 + Response = oauth2_response:new(?ACCESS, ?EXPIRY, ?RESOURCE_OWNER, ?SCOPE, ?REFRESH),
146 154 ?assertEqual([{<<"access_token">>, ?ACCESS},
147 155 {<<"expires_in">>, list_to_binary(integer_to_list(?EXPIRY))},
  156 + {<<"resource_owner">>, ?RESOURCE_OWNER},
148 157 {<<"scope">>, ?SCOPE},
149 158 {<<"refresh_token">>, ?REFRESH},
150 159 {<<"token_type">>, <<"bearer">>}],
2  test/oauth2_tests.erl
@@ -131,6 +131,8 @@ verify_access_code_test_() ->
131 131 ?RESOURCE_OWNER,
132 132 ?CLIENT_SCOPE),
133 133 {ok, Code} = oauth2_response:access_code(Response),
  134 + ?assertMatch({ok, ?RESOURCE_OWNER},
  135 + oauth2_response:resource_owner(Response)),
134 136 ?assertMatch({ok, _}, oauth2:verify_access_code(Code)),
135 137 {ok, _, Response2} = oauth2:authorize_code_grant(
136 138 ?CLIENT_ID,

0 comments on commit f76ec6e

Please sign in to comment.
Something went wrong with that request. Please try again.