Permalink
Browse files

default scopes and richer handler

  • Loading branch information...
1 parent 1ab3941 commit 199327a07157bc230cbf2c9f743520b2bf16326a @dvv committed Mar 22, 2013
View
@@ -21,7 +21,7 @@ Router configuration
{"/auth/google/:action", cowboy_social, [
{provider, cowboy_social_google},
% At the end of the flow this handler will be called as
- % Mod:Fun({ok, Auth, Profile}, Req) or Mod:Fun({error, Reason}, Req)
+ % Mod:Fun({ok, Auth, Profile}, Req, State) or Mod:Fun({error, Reason}, Req, State)
{handler, {Mod, Fun}},
{client_id, <<"...">>},
{client_secret, <<"...">>},
View
@@ -15,21 +15,24 @@
%%
init(_Transport, Req, Opts) ->
-% pecypc_log:info({url, cowboy_req:get([path, qs_vals], Req)}),
% compose full redirect URI
case key(callback_uri, Opts) of
<< "http://", _/binary >> -> {ok, Req, Opts};
<< "https://", _/binary >> -> {ok, Req, Opts};
Relative ->
- {SelfUri, Req2} = cowboy_req:host_url(Req),
- {ok, Req2, lists:keyreplace(callback_uri, 1, Opts,
- {callback_uri, << SelfUri/binary, Relative/binary >>})}
+ {Headers, Req2} = cowboy_req:headers(Req),
+ % NB: we use X-Scheme custom header to honor proxies
+ {ok, Req2, lists:keyreplace(callback_uri, 1, Opts, {callback_uri,
+ << (key(<<"x-scheme">>, Headers, <<"http">>))/binary, "://",
+ (key(<<"host">>, Headers))/binary,
+ Relative/binary >>})}
end.
terminate(_Reason, _Req, _State) ->
ok.
handle(Req, Opts) ->
+% pecypc_log:info({req, Opts}),
% extract flow action name
{Action, Req2} = cowboy_req:binding(action, Req),
% perform flow action
@@ -41,7 +44,6 @@ handle(Req, Opts) ->
%% to our next handler
%%
handle_request(<<"login">>, Req, Opts) ->
-% pecypc_log:info({login, cowboy_req:get([path, qs_vals], Req), Opts}),
cowboy_req:reply(302, [
{<<"location">>, (key(provider, Opts)):get_authorize_url(Opts)}
], <<>>, Req);
@@ -50,7 +52,6 @@ handle_request(<<"login">>, Req, Opts) ->
%% provider redirected back to us with authorization code
%%
handle_request(<<"callback">>, Req, Opts) ->
-% pecypc_log:info({callback, cowboy_req:get([path, qs_vals], Req), Opts}),
case cowboy_req:qs_val(<<"code">>, Req) of
{undefined, Req2} ->
finish({error, nocode}, Req2, Opts);
@@ -89,15 +90,7 @@ get_user_profile(Auth, Req, Opts) ->
%%
finish(Status, Req, Opts) ->
{M, F} = key(handler, Opts),
- {session, Session, SessionOpts} = lists:keyfind(session, 1, Opts),
- Session2 = case Status of
- {ok, Auth, Profile} ->
- [{access_token, key(access_token, Auth)}, {profile, Profile}];
- {error, _Reason} ->
- undefined
- end,
- Req2 = cowboy_cookie_session:set_session(Session2, SessionOpts, Req),
- M:F(Status, Req2).
+ M:F(Status, Req, Opts).
%%
%%------------------------------------------------------------------------------
@@ -106,5 +99,10 @@ finish(Status, Req, Opts) ->
%%
key(Key, List) ->
- {_, Value} = lists:keyfind(Key, 1, List),
- Value.
+ key(Key, List, <<>>).
+
+key(Key, List, Def) ->
+ case lists:keyfind(Key, 1, List) of
+ {_, Value} -> Value;
+ _ -> Def
+ end.
@@ -25,7 +25,7 @@ get_authorize_url(Opts) ->
(cowboy_request:urlencode([
{client_id, key(client_id, Opts)},
{redirect_uri, key(callback_uri, Opts)},
- {scope, key(scope, Opts)}
+ {scope, << "email ", (key(scope, Opts))/binary >>}
]))/binary >>.
%%
@@ -58,7 +58,7 @@ get_user_profile(Auth, _Opts) ->
{ok, [
{id, << "facebook:", (key(<<"id">>, Profile))/binary >>},
{provider, <<"facebook">>},
- % {email, key(<<"email">>, Profile)},
+ {email, key(<<"email">>, Profile)},
{name, key(<<"name">>, Profile)},
{avatar, key(<<"url">>, key(<<"data">>, key(<<"picture">>, Profile)))},
{gender, key(<<"gender">>, Profile)},
@@ -72,5 +72,10 @@ get_user_profile(Auth, _Opts) ->
%%
key(Key, List) ->
- {_, Value} = lists:keyfind(Key, 1, List),
- Value.
+ key(Key, List, <<>>).
+
+key(Key, List, Def) ->
+ case lists:keyfind(Key, 1, List) of
+ {_, Value} -> Value;
+ _ -> Def
+ end.
@@ -69,5 +69,10 @@ get_user_profile(Auth, _Opts) ->
%%
key(Key, List) ->
- {_, Value} = lists:keyfind(Key, 1, List),
- Value.
+ key(Key, List, <<>>).
+
+key(Key, List, Def) ->
+ case lists:keyfind(Key, 1, List) of
+ {_, Value} -> Value;
+ _ -> Def
+ end.
@@ -26,7 +26,9 @@ get_authorize_url(Opts) ->
{client_id, key(client_id, Opts)},
{redirect_uri, key(callback_uri, Opts)},
{response_type, <<"code">>},
- {scope, key(scope, Opts)}
+ {scope, << "https://www.googleapis.com/auth/userinfo.email ",
+ "https://www.googleapis.com/auth/userinfo.profile ",
+ (key(scope, Opts))/binary >>}
]))/binary >>.
%%
@@ -72,5 +74,10 @@ get_user_profile(Auth, _Opts) ->
%%
key(Key, List) ->
- {_, Value} = lists:keyfind(Key, 1, List),
- Value.
+ key(Key, List, <<>>).
+
+key(Key, List, Def) ->
+ case lists:keyfind(Key, 1, List) of
+ {_, Value} -> Value;
+ _ -> Def
+ end.
@@ -82,8 +82,13 @@ get_user_profile(Auth, Opts) ->
%%
key(Key, List) ->
- {_, Value} = lists:keyfind(Key, 1, List),
- Value.
+ key(Key, List, <<>>).
+
+key(Key, List, Def) ->
+ case lists:keyfind(Key, 1, List) of
+ {_, Value} -> Value;
+ _ -> Def
+ end.
md5hex(Bin) ->
list_to_binary(lists:flatten([io_lib:format("~2.16.0b",[N]) ||
@@ -26,7 +26,8 @@ get_authorize_url(Opts) ->
{client_id, key(client_id, Opts)},
{redirect_uri, key(callback_uri, Opts)},
{response_type, <<"code">>},
- {scope, key(scope, Opts)}
+ {scope, << "https://identity.x.com/xidentity/resources/profile/me ",
+ (key(scope, Opts))/binary >>}
]))/binary >>.
%%
@@ -71,5 +72,10 @@ get_user_profile(Auth, _Opts) ->
%%
key(Key, List) ->
- {_, Value} = lists:keyfind(Key, 1, List),
- Value.
+ key(Key, List, <<>>).
+
+key(Key, List, Def) ->
+ case lists:keyfind(Key, 1, List) of
+ {_, Value} -> Value;
+ _ -> Def
+ end.
@@ -26,7 +26,8 @@ get_authorize_url(Opts) ->
{client_id, key(client_id, Opts)},
{redirect_uri, key(callback_uri, Opts)},
{response_type, <<"code">>},
- {scope, key(scope, Opts)}
+ {scope, << "uid,first_name,last_name,sex,photo ",
+ (key(scope, Opts))/binary >>}
]))/binary >>.
%%
@@ -77,5 +78,10 @@ get_user_profile(Auth, Opts) ->
%%
key(Key, List) ->
- {_, Value} = lists:keyfind(Key, 1, List),
- Value.
+ key(Key, List, <<>>).
+
+key(Key, List, Def) ->
+ case lists:keyfind(Key, 1, List) of
+ {_, Value} -> Value;
+ _ -> Def
+ end.
@@ -73,5 +73,10 @@ get_user_profile(Auth, _Opts) ->
%%
key(Key, List) ->
- {_, Value} = lists:keyfind(Key, 1, List),
- Value.
+ key(Key, List, <<>>).
+
+key(Key, List, Def) ->
+ case lists:keyfind(Key, 1, List) of
+ {_, Value} -> Value;
+ _ -> Def
+ end.

0 comments on commit 199327a

Please sign in to comment.