Skip to content

Commit

Permalink
use single handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dvv committed Jun 14, 2013
1 parent d95ee22 commit c23ba4d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 84 deletions.
130 changes: 61 additions & 69 deletions README.md
Expand Up @@ -18,74 +18,66 @@ Router configuration
--------------

```erlang
%
% Handle authorization
%
{"/auth/facebook/:action", cowboy_social, [
{provider, facebook},
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/facebook/callback">>},
{scope, <<"email">>},
{authorize_uri, <<"https://www.facebook.com/dialog/oauth">>},
{token_uri, <<"https://graph.facebook.com/oauth/access_token">>}
]},
{"/auth/github/:action", cowboy_social, [
{provider, github},
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/github/callback">>},
{scope, <<>>},
{authorize_uri, <<"https://github.com/login/oauth/authorize">>},
{token_uri, <<"https://github.com/login/oauth/access_token">>}
]},
{"/auth/google/:action", cowboy_social, [
{provider, google},
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/google/callback">>},
{scope, << "https://www.googleapis.com/auth/userinfo.email ",
"https://www.googleapis.com/auth/userinfo.profile" >>},
{authorize_uri, <<"https://accounts.google.com/o/oauth2/auth">>},
{token_uri, <<"https://accounts.google.com/o/oauth2/token">>}
]},
{"/auth/mailru/:action", cowboy_social, [
{provider, mailru},
{client_id, <<"...">>},
{client_secret, <<"...">>},
{secret_key, <<"f431aea09762dbad13c2440955e12aee">>},
{callback_uri, <<"/auth/mailru/callback">>},
{scope, <<>>},
{authorize_uri, <<"https://connect.mail.ru/oauth/authorize">>},
{token_uri, <<"https://connect.mail.ru/oauth/token">>}
]},
{"/auth/paypal/:action", cowboy_social, [
{provider, paypal},
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/paypal/callback">>},
{scope, <<"https://identity.x.com/xidentity/resources/profile/me">>},
{authorize_uri, <<"https://identity.x.com/xidentity/resources/authorize">>},
{token_uri, <<"https://identity.x.com/xidentity/oauthtokenservice">>}
]},
{"/auth/vkontakte/:action", cowboy_social, [
{provider, vkontakte},
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/vkontakte/callback">>},
{scope, <<"uid,first_name,last_name,sex,photo">>},
{authorize_uri, <<"https://oauth.vk.com/authorize">>},
{token_uri, <<"https://oauth.vk.com/access_token">>}
]},
{"/auth/yandex/:action", cowboy_social, [
{provider, yandex},
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/yandex/callback">>},
{scope, <<>>},
{authorize_uri, <<"https://oauth.yandex.ru/authorize">>},
{token_uri, <<"https://oauth.yandex.ru/token">>}
]}.
{"/auth/:provider/:action", cowboy_social, [
{<<"facebook">>, [
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/facebook/callback">>},
{scope, <<"email">>},
{authorize_uri, <<"https://www.facebook.com/dialog/oauth">>},
{token_uri, <<"https://graph.facebook.com/oauth/access_token">>}
]},
{<<"github">>, [
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/github/callback">>},
{scope, <<>>},
{authorize_uri, <<"https://github.com/login/oauth/authorize">>},
{token_uri, <<"https://github.com/login/oauth/access_token">>}
]},
{<<"google">>, [
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/google/callback">>},
{scope, << "https://www.googleapis.com/auth/userinfo.email ",
"https://www.googleapis.com/auth/userinfo.profile" >>},
{authorize_uri, <<"https://accounts.google.com/o/oauth2/auth">>},
{token_uri, <<"https://accounts.google.com/o/oauth2/token">>}
]},
{<<"mailru">>, [
{client_id, <<"...">>},
{client_secret, <<"...">>},
{secret_key, <<"...">>},
{callback_uri, <<"/auth/mailru/callback">>},
{scope, <<>>},
{authorize_uri, <<"https://connect.mail.ru/oauth/authorize">>},
{token_uri, <<"https://connect.mail.ru/oauth/token">>}
]},
{<<"paypal">>, [
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/paypal/callback">>},
{scope, <<"https://identity.x.com/xidentity/resources/profile/me">>},
{authorize_uri, <<"https://identity.x.com/xidentity/resources/authorize">>},
{token_uri, <<"https://identity.x.com/xidentity/oauthtokenservice">>}
]},
{<<"vkontakte">>, [
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/vkontakte/callback">>},
{scope, <<"uid,first_name,last_name,sex,photo">>},
{authorize_uri, <<"https://oauth.vk.com/authorize">>},
{token_uri, <<"https://oauth.vk.com/access_token">>}
]},
{<<"yandex">>, [
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/yandex/callback">>},
{scope, <<>>},
{authorize_uri, <<"https://oauth.yandex.ru/authorize">>},
{token_uri, <<"https://oauth.yandex.ru/token">>}
]}
]}.
```

Supported providers
Expand All @@ -112,7 +104,7 @@ function try_login(provider) {
clearInterval(poller);
old_atoken = window.atoken;
// use window.atoken.access_token hereafter to access secured resource
$.getJSON('/api/' + provider + '/user_profile', {access_token: old_atoken.access_token}, function (profile) {
$.getJSON('/auth/' + provider + '/user_profile', {access_token: old_atoken.access_token}, function (profile) {
// use social profile here
console.log(profile);
});
Expand Down
35 changes: 20 additions & 15 deletions src/cowboy_social.erl
Expand Up @@ -23,15 +23,24 @@
-record(state, {
action,
options,
provider,
token
}).

init(_Transport, Req, Opts) ->
{Action, Req2} = cowboy_req:binding(action, Req, <<"login">>),
{upgrade, protocol, cowboy_rest, Req2, #state{
action = Action,
options = Opts
}}.
{Provider, Req2} = cowboy_req:binding(provider, Req),
{Action, Req3} = cowboy_req:binding(action, Req2, <<"login">>),
case lists:keyfind(Provider, 1, Opts) of
false ->
{ok, Req4} = cowboy_req:reply(404, Req3),
{shutdown, Req4, undefined};
{_, O} ->
{upgrade, protocol, cowboy_rest, Req3, #state{
action = Action,
options = O,
provider = Provider
}}
end.

terminate(_Reason, _Req, _State) ->
ok.
Expand Down Expand Up @@ -120,11 +129,6 @@ action(Req, #state{action = <<"login">>, options = O}) ->
{Type, Req2} = cowboy_req:qs_val(<<"response_type">>, Req, <<"code">>),
{Opaque, Req3} = cowboy_req:qs_val(<<"state">>, Req2, <<>>),
% redirect to provider authorization page
% Mod = binary_to_atom(<< "cowboy_social_", Provider/binary >>, latin1),
% {ok, Req4} = cowboy_req:reply(302, [
% {<<"location">>, Mod:authorize(Opts)}
% ], <<>>, Req3),
% {halt, Req4, State};
redirect(key(authorize_uri, O), [
{client_id, key(client_id, O)},
{redirect_uri, key(callback_uri, O)},
Expand All @@ -145,12 +149,13 @@ action(Req, State = #state{action = <<"callback">>}) ->
{error, Error, Req2}
end;

action(Req, #state{action = Action, token = Token, options = O}) ->
% @fixme atoms are not purged!
{_, Provider} = lists:keyfind(provider, 1, O),
%%
%% Protected actions.
%%
action(Req, #state{action = Action, token = Token, options = O,
provider = Provider}) ->
case apply(
binary_to_atom(<<
"cowboy_social_", (atom_to_binary(Provider, latin1))/binary >>, latin1),
binary_to_atom(<< "cowboy_social_", Provider/binary >>, latin1),
binary_to_atom(Action, latin1),
[Token, O])
of
Expand Down

0 comments on commit c23ba4d

Please sign in to comment.