Permalink
Browse files

drop separate profile handler

  • Loading branch information...
1 parent b4fdb5e commit d95ee22b4b5f3dc6c8e5faf9e8c5b6598cbac253 @dvv committed Jun 14, 2013
Showing with 46 additions and 78 deletions.
  1. +10 −1 README.md
  2. +36 −1 src/cowboy_social.erl
  3. +0 −76 src/cowboy_social_profile.erl
View
@@ -18,8 +18,11 @@ Router configuration
--------------
```erlang
-% Use :action binding
+%
+% Handle authorization
+%
{"/auth/facebook/:action", cowboy_social, [
+ {provider, facebook},
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/facebook/callback">>},
@@ -28,6 +31,7 @@ Router configuration
{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">>},
@@ -36,6 +40,7 @@ Router configuration
{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">>},
@@ -45,6 +50,7 @@ Router configuration
{token_uri, <<"https://accounts.google.com/o/oauth2/token">>}
]},
{"/auth/mailru/:action", cowboy_social, [
+ {provider, mailru},
{client_id, <<"...">>},
{client_secret, <<"...">>},
{secret_key, <<"f431aea09762dbad13c2440955e12aee">>},
@@ -54,6 +60,7 @@ Router configuration
{token_uri, <<"https://connect.mail.ru/oauth/token">>}
]},
{"/auth/paypal/:action", cowboy_social, [
+ {provider, paypal},
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/paypal/callback">>},
@@ -62,6 +69,7 @@ Router configuration
{token_uri, <<"https://identity.x.com/xidentity/oauthtokenservice">>}
]},
{"/auth/vkontakte/:action", cowboy_social, [
+ {provider, vkontakte},
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/vkontakte/callback">>},
@@ -70,6 +78,7 @@ Router configuration
{token_uri, <<"https://oauth.vk.com/access_token">>}
]},
{"/auth/yandex/:action", cowboy_social, [
+ {provider, yandex},
{client_id, <<"...">>},
{client_secret, <<"...">>},
{callback_uri, <<"/auth/yandex/callback">>},
View
@@ -11,6 +11,7 @@
terminate/3,
rest_init/2,
allowed_methods/2,
+ is_authorized/2,
content_types_provided/2
]).
@@ -21,7 +22,8 @@
-record(state, {
action,
- options
+ options,
+ token
}).
init(_Transport, Req, Opts) ->
@@ -56,6 +58,24 @@ rest_init(Req, State = #state{options = O}) ->
allowed_methods(Req, State) ->
{[<<"GET">>], Req, State}.
+%% NB: authorization is required for protected actions
+is_authorized(Req, State = #state{action = <<"user_profile">>}) ->
+ case cowboy_req:header(<<"authorization">>, Req) of
+ {<< "Bearer ", Bearer/binary >>, Req2} ->
+ {true, Req2, State#state{token = Bearer}};
+ {undefined, Req2} ->
+ case cowboy_req:qs_val(<<"access_token">>, Req2) of
+ {undefined, Req3} ->
+ {{false, <<"Bearer">>}, Req3, State};
+ {Token, Req3} ->
+ {true, Req3, State#state{token = Token}}
+ end;
+ {_, Req2} ->
+ {{false, <<"Bearer">>}, Req2, State}
+ end;
+is_authorized(Req, State) ->
+ {true, Req, State}.
+
content_types_provided(Req, State) ->
{[
{{<<"text">>, <<"html">>, []}, get_html},
@@ -123,6 +143,21 @@ action(Req, State = #state{action = <<"callback">>}) ->
check_code(Req2, State);
{Error, Req2} ->
{error, Error, Req2}
+ end;
+
+action(Req, #state{action = Action, token = Token, options = O}) ->
+ % @fixme atoms are not purged!
+ {_, Provider} = lists:keyfind(provider, 1, O),
+ case apply(
+ binary_to_atom(<<
+ "cowboy_social_", (atom_to_binary(Provider, latin1))/binary >>, latin1),
+ binary_to_atom(Action, latin1),
+ [Token, O])
+ of
+ {ok, Result} ->
+ {ok, Result, Req};
+ {error, Error} ->
+ {error, Error, Req}
end.
check_code(Req, State = #state{options = O}) ->
@@ -1,76 +0,0 @@
-%%
-%% @doc Handler for calling social providers API.
-%%
-
--module(cowboy_social_profile).
--author('Vladimir Dronnikov <dronnikov@gmail.com>').
-
-% -behaviour(cowboy_rest_handler).
--export([
- init/3,
- terminate/3,
- rest_init/2,
- is_authorized/2,
- content_types_provided/2
- ]).
-
--export([
- get_json/2
- ]).
-
--record(state, {
- action,
- options,
- token
- }).
-
-init(_Transport, Req, Opts) ->
- {Action, Req2} = cowboy_req:binding(action, Req, <<"login">>),
- {upgrade, protocol, cowboy_rest, Req2, #state{
- action = Action,
- options = Opts
- }}.
-
-terminate(_Reason, _Req, _State) ->
- ok.
-
-rest_init(Req, State) ->
- {ok, Req, State}.
-
-%%
-%% `Authorization: Bearer TOKEN` or `?access_token=TOKEN` required
-%%
-is_authorized(Req, State) ->
- case cowboy_req:header(<<"authorization">>, Req) of
- {<< "Bearer ", Bearer/binary >>, Req2} ->
- {true, Req2, State#state{token = Bearer}};
- {undefined, Req2} ->
- case cowboy_req:qs_val(<<"access_token">>, Req2) of
- {undefined, Req3} ->
- {{false, <<"Bearer">>}, Req3, State};
- {Token, Req3} ->
- {true, Req3, State#state{token = Token}}
- end;
- {_, Req2} ->
- {{false, <<"Bearer">>}, Req2, State}
- end.
-
-content_types_provided(Req, State) ->
- {[
- {{<<"application">>, <<"json">>, []}, get_json}
- ], Req, State}.
-
-get_json(Req, State = #state{action = Action, options = Opts, token = Token}) ->
- % @fixme atoms are not purged!
- {_, Provider} = lists:keyfind(provider, 1, Opts),
- case apply(
- binary_to_atom(<<
- "cowboy_social_", (atom_to_binary(Provider, latin1))/binary >>, latin1),
- binary_to_atom(Action, latin1),
- [Token, Opts])
- of
- {ok, Result} ->
- {jsx:encode(Result), Req, State};
- {error, Error} ->
- {jsx:encode([{error, Error}]), Req, State}
- end.

0 comments on commit d95ee22

Please sign in to comment.