Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Style

  • Loading branch information...
commit 7e3905bfa3016586d51559f2ea12a84dba4f60c1 1 parent 5cb7f93
@dvv authored
View
192 src/cowboy_request.erl
@@ -1,96 +1,96 @@
-%%
-%%------------------------------------------------------------------------------
-%% !!! Hackish reuse of cowboy_client undocumented interface !!!
-%%------------------------------------------------------------------------------
-%%
-
--module(cowboy_request).
--author('Vladimir Dronnikov <dronnikov@gmail.com>').
-
--export([get_json/2]).
--export([post_for_json/2]).
--export([request/4]).
--export([urlencode/1]).
-
--record(client, {
- state = wait :: wait | request | response | response_body,
- opts = [] :: [any()],
- socket = undefined :: undefined | inet:socket(),
- transport = undefined :: module(),
- timeout = 5000 :: timeout(), %% @todo Configurable.
- buffer = <<>> :: binary(),
- connection = keepalive :: keepalive | close,
- version = {1, 1} :: cowboy_http:version(),
- response_body = undefined :: undefined | non_neg_integer()
-}).
-
-request(Method, URL, Headers, Body) ->
-% pecypc_log:info({req, Method, URL, Body}),
- {ok, Client0} = cowboy_client:init([]),
- % NB: have to degrade protocol to not allow chunked responses
- Client = Client0#client{version = {1, 0}},
- {ok, Client2} = cowboy_client:request(Method, URL, [
- {<<"connection">>, <<"close">>},
- {<<"accept-encoding">>, <<"identity">>},
- {<<"accept">>, <<"application/json">>},
- {<<"pragma">>, <<"no-cache">>},
- {<<"cache-control">>,
- <<"private, max-age: 0, no-cache, must-revalidate">>}
- | Headers
- ], Body, Client),
- Result = case cowboy_client:response(Client2) of
- {ok, 200, _ResHeaders, Client3} ->
- case Client3#client.state of
- % @fixme dirty hack, reports only first read chunk
- request ->
- {ok, Client3#client.buffer};
- response_body ->
- case cowboy_client:response_body(Client3) of
- {ok, ResBody, _} ->
- {ok, ResBody};
- Else ->
- Else
- end
- end;
- _Else ->
-% pecypc_log:info({reqerr, _Else}),
- {error, failed}
- end,
-% pecypc_log:info({res, _ResHeaders, Result}),
- Result.
-
-urlencode(Bin) when is_binary(Bin) ->
- cowboy_http:urlencode(Bin);
-urlencode(Atom) when is_atom(Atom) ->
- urlencode(atom_to_binary(Atom, latin1));
-urlencode(List) when is_list(List) ->
- urlencode(List, <<>>).
-urlencode([], Acc) ->
- Acc;
-urlencode([{K, V} | T], <<>>) ->
- urlencode(T, << (urlencode(K))/binary, $=, (urlencode(V))/binary >>);
-urlencode([{K, V} | T], Acc) ->
- urlencode(T, << Acc/binary, $&,
- (urlencode(K))/binary, $=, (urlencode(V))/binary >>).
-
-parse({ok, JSON}) ->
- case jsx:decode(JSON, [{error_handler, fun(_, _, _) -> {error, badarg} end}])
- of
- {error, _} ->
- % {ok, cowboy_http:x_www_form_urlencoded(JSON)};
- {error, badarg};
- Hash ->
- {ok, Hash}
- end;
-parse(_) ->
- {error, badarg}.
-
-get_json(URL, Data) ->
- parse(request(<<"GET">>,
- << URL/binary, $?,
- (urlencode(Data))/binary >>, [], <<>>)).
-
-post_for_json(URL, Data) ->
- parse(request(<<"POST">>, URL, [
- {<<"content-type">>, <<"application/x-www-form-urlencoded">>}
- ], urlencode(Data))).
+%%
+%%------------------------------------------------------------------------------
+%% !!! Hackish reuse of cowboy_client undocumented interface !!!
+%%------------------------------------------------------------------------------
+%%
+
+-module(cowboy_request).
+-author('Vladimir Dronnikov <dronnikov@gmail.com>').
+
+-export([get_json/2]).
+-export([post_for_json/2]).
+-export([request/4]).
+-export([urlencode/1]).
+
+-record(client, {
+ state = wait :: wait | request | response | response_body,
+ opts = [] :: [any()],
+ socket = undefined :: undefined | inet:socket(),
+ transport = undefined :: module(),
+ timeout = 5000 :: timeout(), %% @todo Configurable.
+ buffer = <<>> :: binary(),
+ connection = keepalive :: keepalive | close,
+ version = {1, 1} :: cowboy_http:version(),
+ response_body = undefined :: undefined | non_neg_integer()
+}).
+
+request(Method, URL, Headers, Body) ->
+% pecypc_log:info({req, Method, URL, Body}),
+ {ok, Client0} = cowboy_client:init([]),
+ % NB: have to degrade protocol to not allow chunked responses
+ Client = Client0#client{version = {1, 0}},
+ {ok, Client2} = cowboy_client:request(Method, URL, [
+ {<<"connection">>, <<"close">>},
+ {<<"accept-encoding">>, <<"identity">>},
+ {<<"accept">>, <<"application/json">>},
+ {<<"pragma">>, <<"no-cache">>},
+ {<<"cache-control">>,
+ <<"private, max-age: 0, no-cache, must-revalidate">>}
+ | Headers
+ ], Body, Client),
+ Result = case cowboy_client:response(Client2) of
+ {ok, 200, _ResHeaders, Client3} ->
+ case Client3#client.state of
+ % @fixme dirty hack, reports only first read chunk
+ request ->
+ {ok, Client3#client.buffer};
+ response_body ->
+ case cowboy_client:response_body(Client3) of
+ {ok, ResBody, _} ->
+ {ok, ResBody};
+ Else ->
+ Else
+ end
+ end;
+ _Else ->
+% pecypc_log:info({reqerr, _Else}),
+ {error, failed}
+ end,
+% pecypc_log:info({res, _ResHeaders, Result}),
+ Result.
+
+urlencode(Bin) when is_binary(Bin) ->
+ cowboy_http:urlencode(Bin);
+urlencode(Atom) when is_atom(Atom) ->
+ urlencode(atom_to_binary(Atom, latin1));
+urlencode(List) when is_list(List) ->
+ urlencode(List, <<>>).
+urlencode([], Acc) ->
+ Acc;
+urlencode([{K, V} | T], <<>>) ->
+ urlencode(T, << (urlencode(K))/binary, $=, (urlencode(V))/binary >>);
+urlencode([{K, V} | T], Acc) ->
+ urlencode(T, << Acc/binary, $&,
+ (urlencode(K))/binary, $=, (urlencode(V))/binary >>).
+
+parse({ok, JSON}) ->
+ case jsx:decode(JSON, [{error_handler, fun(_, _, _) -> {error, badarg} end}])
+ of
+ {error, _} ->
+ % {ok, cowboy_http:x_www_form_urlencoded(JSON)};
+ {error, badarg};
+ Hash ->
+ {ok, Hash}
+ end;
+parse(_) ->
+ {error, badarg}.
+
+get_json(URL, Data) ->
+ parse(request(<<"GET">>,
+ << URL/binary, $?,
+ (urlencode(Data))/binary >>, [], <<>>)).
+
+post_for_json(URL, Data) ->
+ parse(request(<<"POST">>, URL, [
+ {<<"content-type">>, <<"application/x-www-form-urlencoded">>}
+ ], urlencode(Data))).
View
240 src/cowboy_social.erl
@@ -1,120 +1,120 @@
-%%
-%% @doc Handler for social login via OAuth2 providers.
-%%
-
--module(cowboy_social).
--author('Vladimir Dronnikov <dronnikov@gmail.com>').
-
--behaviour(cowboy_http_handler).
--export([init/3, terminate/3, handle/2]).
-
-%%
-%%------------------------------------------------------------------------------
-%% OAUTH2 Application flow
-%%------------------------------------------------------------------------------
-%%
-
-init(_Transport, Req, Opts) ->
- {ok, Req, Opts}.
-
-terminate(_Reason, _Req, _State) ->
- ok.
-
-%%
-%% {"/auth/:provider/:action", cowboy_social, [...]}.
-%%
-handle(Req, Opts) ->
- Provider = key(provider, Opts),
- % extract flow action name
- {Action, Req2} = cowboy_req:binding(action, Req),
- % construct callback URI
- {SelfUri, Req3} = cowboy_req:host_url(Req2),
- [FullPath] = cowboy_req:get([path], Req3),
- CallbackUrl = << SelfUri/binary, FullPath/binary >>,
- % perform flow action
- {ok, Req4} = handle_request(Action, Provider, Opts, CallbackUrl, Req3),
- {ok, Req4, undefined}.
-
-%%
-%% redirect to provider authorization page, expect it to redirect
-%% to our next handler
-%%
-handle_request(<<"login">>, P, O, U, Req) ->
- AuthUrl = << (cowboy_social_providers:authorize_url(P))/binary, $?,
- (cowboy_request:urlencode([
- {<<"client_id">>, key(client_id, O)},
- {<<"redirect_uri">>, binary:replace(U, <<"/login">>, <<"/callback">>)},
- {<<"response_type">>, <<"code">>},
- {<<"scope">>, key(scope, O)}
- ]))/binary >>,
- cowboy_req:reply(303, [{<<"location">>, AuthUrl}], <<>>, Req);
-
-%%
-%% provider redirected back to us with authorization code
-%%
-handle_request(<<"callback">>, P, O, U, Req) ->
- case cowboy_req:qs_val(<<"code">>, Req) of
- {undefined, Req2} ->
- finish({error, nocode}, Req2);
- {Code, Req2} ->
- get_access_token(P, O, U, Code, Req2)
- end;
-
-%%
-%% catchall
-%%
-handle_request(_, _, _, _, Req) ->
- {ok, Req2} = cowboy_req:reply(404, [], <<>>, Req),
- {ok, Req2, undefined}.
-
-%%
-%% exchange authorization code for auth token
-%%
-get_access_token(P, O, U, Code, Req) ->
- case cowboy_request:post_for_json(cowboy_social_providers:token_url(P), [
- {<<"code">>, Code},
- {<<"client_id">>, key(client_id, O)},
- {<<"client_secret">>, key(client_secret, O)},
- {<<"redirect_uri">>, U},
- {<<"grant_type">>, <<"authorization_code">>}
- ])
- of
- {ok, Auth} ->
- get_user_profile(P, O, Auth, Req);
- _ ->
- finish({error, notoken}, Req)
- end.
-
-%%
-%% use auth tocken to extract info from user profile
-%%
-get_user_profile(P, O, Auth, Req) ->
- AccessToken = key(<<"access_token">>, Auth),
- case cowboy_request:get_json(cowboy_social_providers:profile_url(P), [
- {<<"access_token">>, AccessToken}
- | cowboy_social_providers:custom_data(P, AccessToken, O)
- ])
- of
- {ok, Profile} ->
- finish({ok, cowboy_social_providers:normalize_profile(P, Auth, Profile)},
- Req);
- _ ->
- finish({error, noprofile}, Req)
- end.
-
-%%
-%% finalize application flow by calling callback handler
-%%
-finish(Status, Req) ->
- {{M, F}, Req2} = cowboy_req:meta(callback, Req),
- M:F(Status, Req2).
-
-%%
-%%------------------------------------------------------------------------------
-%% Helpers
-%%------------------------------------------------------------------------------
-%%
-
-key(Key, List) ->
- {_, Value} = lists:keyfind(Key, 1, List),
- Value.
+%%
+%% @doc Handler for social login via OAuth2 providers.
+%%
+
+-module(cowboy_social).
+-author('Vladimir Dronnikov <dronnikov@gmail.com>').
+
+-behaviour(cowboy_http_handler).
+-export([init/3, terminate/3, handle/2]).
+
+%%
+%%------------------------------------------------------------------------------
+%% OAUTH2 Application flow
+%%------------------------------------------------------------------------------
+%%
+
+init(_Transport, Req, Opts) ->
+ {ok, Req, Opts}.
+
+terminate(_Reason, _Req, _State) ->
+ ok.
+
+%%
+%% {"/auth/:provider/:action", cowboy_social, [...]}.
+%%
+handle(Req, Opts) ->
+ Provider = key(provider, Opts),
+ % extract flow action name
+ {Action, Req2} = cowboy_req:binding(action, Req),
+ % construct callback URI
+ {SelfUri, Req3} = cowboy_req:host_url(Req2),
+ [FullPath] = cowboy_req:get([path], Req3),
+ CallbackUrl = << SelfUri/binary, FullPath/binary >>,
+ % perform flow action
+ {ok, Req4} = handle_request(Action, Provider, Opts, CallbackUrl, Req3),
+ {ok, Req4, undefined}.
+
+%%
+%% redirect to provider authorization page, expect it to redirect
+%% to our next handler
+%%
+handle_request(<<"login">>, P, O, U, Req) ->
+ AuthUrl = << (cowboy_social_providers:authorize_url(P))/binary, $?,
+ (cowboy_request:urlencode([
+ {<<"client_id">>, key(client_id, O)},
+ {<<"redirect_uri">>, binary:replace(U, <<"/login">>, <<"/callback">>)},
+ {<<"response_type">>, <<"code">>},
+ {<<"scope">>, key(scope, O)}
+ ]))/binary >>,
+ cowboy_req:reply(303, [{<<"location">>, AuthUrl}], <<>>, Req);
+
+%%
+%% provider redirected back to us with authorization code
+%%
+handle_request(<<"callback">>, P, O, U, Req) ->
+ case cowboy_req:qs_val(<<"code">>, Req) of
+ {undefined, Req2} ->
+ finish({error, nocode}, Req2);
+ {Code, Req2} ->
+ get_access_token(P, O, U, Code, Req2)
+ end;
+
+%%
+%% catchall
+%%
+handle_request(_, _, _, _, Req) ->
+ {ok, Req2} = cowboy_req:reply(404, [], <<>>, Req),
+ {ok, Req2, undefined}.
+
+%%
+%% exchange authorization code for auth token
+%%
+get_access_token(P, O, U, Code, Req) ->
+ case cowboy_request:post_for_json(cowboy_social_providers:token_url(P), [
+ {<<"code">>, Code},
+ {<<"client_id">>, key(client_id, O)},
+ {<<"client_secret">>, key(client_secret, O)},
+ {<<"redirect_uri">>, U},
+ {<<"grant_type">>, <<"authorization_code">>}
+ ])
+ of
+ {ok, Auth} ->
+ get_user_profile(P, O, Auth, Req);
+ _ ->
+ finish({error, notoken}, Req)
+ end.
+
+%%
+%% use auth tocken to extract info from user profile
+%%
+get_user_profile(P, O, Auth, Req) ->
+ AccessToken = key(<<"access_token">>, Auth),
+ case cowboy_request:get_json(cowboy_social_providers:profile_url(P), [
+ {<<"access_token">>, AccessToken}
+ | cowboy_social_providers:custom_data(P, AccessToken, O)
+ ])
+ of
+ {ok, Profile} ->
+ finish({ok, cowboy_social_providers:normalize_profile(P, Auth, Profile)},
+ Req);
+ _ ->
+ finish({error, noprofile}, Req)
+ end.
+
+%%
+%% finalize application flow by calling callback handler
+%%
+finish(Status, Req) ->
+ {{M, F}, Req2} = cowboy_req:meta(callback, Req),
+ M:F(Status, Req2).
+
+%%
+%%------------------------------------------------------------------------------
+%% Helpers
+%%------------------------------------------------------------------------------
+%%
+
+key(Key, List) ->
+ {_, Value} = lists:keyfind(Key, 1, List),
+ Value.
View
276 src/cowboy_social_google.erl
@@ -1,139 +1,137 @@
-%%
-%% @doc Handler for social login via OAuth2 providers.
-%%
-
--module(cowboy_social_google).
--author('Vladimir Dronnikov <dronnikov@gmail.com>').
-
--behaviour(cowboy_http_handler).
--export([init/3, terminate/3, handle/2]).
-
-%%
-%%------------------------------------------------------------------------------
-%% OAUTH2 Application flow
-%%------------------------------------------------------------------------------
-%%
-
-init(_Transport, Req, Opts) ->
- {ok, Req, Opts}.
-
-terminate(_Reason, _Req, _State) ->
- ok.
-
-%%
-%% {"/auth/google/:action", cowboy_social_google, [...]}.
-%%
-handle(Req, Opts) ->
- % extract flow action name
- {Action, Req2} = cowboy_req:binding(action, Req),
- % construct callback URI
- {SelfUri, Req3} = cowboy_req:host_url(Req2),
- [FullPath] = cowboy_req:get([path], Req3),
- CallbackUrl = << SelfUri/binary, FullPath/binary >>,
- % perform flow action
- {ok, Req4} = handle_request(Action, Opts, CallbackUrl, Req3),
- {ok, Req4, undefined}.
-
-%%
-%% redirect to provider authorization page, expect it to redirect
-%% to our next handler
-%%
-handle_request(<<"login">>, Opts, U, Req) ->
- AuthUrl = << (authorize_url())/binary, $?,
- (cowboy_request:urlencode([
- {<<"client_id">>, key(client_id, Opts)},
- {<<"redirect_uri">>, binary:replace(U, <<"/login">>, <<"/callback">>)},
- {<<"response_type">>, <<"code">>},
- {<<"scope">>, key(scope, Opts)}
- ]))/binary >>,
- cowboy_req:reply(303, [{<<"location">>, AuthUrl}], <<>>, Req);
-
-%%
-%% provider redirected back to us with authorization code
-%%
-handle_request(<<"callback">>, Opts, U, Req) ->
- case cowboy_req:qs_val(<<"code">>, Req) of
- {undefined, Req2} ->
- finish({error, nocode}, Req2, Opts);
- {Code, Req2} ->
- get_access_token(Opts, U, Code, Req2)
- end;
-
-%%
-%% catchall
-%%
-handle_request(_, _, _, Req) ->
- {ok, Req2} = cowboy_req:reply(404, [], <<>>, Req),
- {ok, Req2, undefined}.
-
-%%
-%% exchange authorization code for auth token
-%%
-get_access_token(Opts, U, Code, Req) ->
- case cowboy_request:post_for_json(token_url(), [
- {<<"code">>, Code},
- {<<"client_id">>, key(client_id, Opts)},
- {<<"client_secret">>, key(client_secret, Opts)},
- {<<"redirect_uri">>, U},
- {<<"grant_type">>, <<"authorization_code">>}
- ])
- of
- {ok, Auth} ->
- get_user_profile(Opts, Auth, Req);
- _ ->
- finish({error, notoken}, Req, Opts)
- end.
-
-%%
-%% use auth tocken to extract info from user profile
-%%
-get_user_profile(Opts, Auth, Req) ->
- AccessToken = key(<<"access_token">>, Auth),
- case cowboy_request:get_json(profile_url(), [
- {<<"access_token">>, AccessToken}
- ])
- of
- {ok, Profile} ->
- finish({ok, normalize_profile(Auth, Profile)},
- Req, Opts);
- _ ->
- finish({error, noprofile}, Req, Opts)
- end.
-
-%%
-%% finalize application flow by calling callback handler
-%%
-finish(Status, Req, Opts) ->
- {M, F} = key(callback, Opts),
- M:F(Status, Req).
-
-%%
-%%------------------------------------------------------------------------------
-%% Helpers
-%%------------------------------------------------------------------------------
-%%
-
-key(Key, List) ->
- {_, Value} = lists:keyfind(Key, 1, List),
- Value.
-
-authorize_url() ->
- <<"https://accounts.google.com/o/oauth2/auth">>.
-
-token_url() ->
- <<"https://accounts.google.com/o/oauth2/token">>.
-
-profile_url() ->
- <<"https://www.googleapis.com/oauth2/v1/userinfo">>.
-
-normalize_profile(_A, P) ->
- [
- {id, << "google:", (key(<<"id">>, P))/binary >>},
- {provider, <<"google">>},
- {email, key(<<"email">>, P)},
- {name, key(<<"name">>, P)},
- {avatar, key(<<"picture">>, P)},
- {gender, key(<<"gender">>, P)},
- {locale, key(<<"locale">>, P)}
- ].
-
+%%
+%% @doc Handler for social login via OAuth2 providers.
+%%
+
+-module(cowboy_social_google).
+-author('Vladimir Dronnikov <dronnikov@gmail.com>').
+
+-behaviour(cowboy_http_handler).
+-export([init/3, terminate/3, handle/2]).
+
+%%
+%%------------------------------------------------------------------------------
+%% OAUTH2 Application flow
+%%------------------------------------------------------------------------------
+%%
+
+init(_Transport, Req, Opts) ->
+ {ok, Req, Opts}.
+
+terminate(_Reason, _Req, _State) ->
+ ok.
+
+%%
+%% {"/auth/google/:action", cowboy_social_google, [...]}.
+%%
+handle(Req, Opts) ->
+ % extract flow action name
+ {Action, Req2} = cowboy_req:binding(action, Req),
+ % perform flow action
+ {ok, Req3} = handle_request(Action, Req2, Opts),
+ {ok, Req3, undefined}.
+
+%%
+%% redirect to provider authorization page, expect it to redirect
+%% to our next handler
+%%
+handle_request(<<"login">>, Req, Opts) ->
+ {SelfUri, Req2} = cowboy_req:host_url(Req),
+ CallbackUrl = << SelfUri/binary, (key(callback_uri, Opts))/binary >>,
+ AuthUrl = << (authorize_url())/binary, $?,
+ (cowboy_request:urlencode([
+ {<<"client_id">>, key(client_id, Opts)},
+ {<<"redirect_uri">>, CallbackUrl},
+ {<<"response_type">>, <<"code">>},
+ {<<"scope">>, key(scope, Opts)}
+ ]))/binary >>,
+ cowboy_req:reply(303, [{<<"location">>, AuthUrl}], <<>>, Req2);
+
+%%
+%% provider redirected back to us with authorization code
+%%
+handle_request(<<"callback">>, Req, Opts) ->
+ case cowboy_req:qs_val(<<"code">>, Req) of
+ {undefined, Req2} ->
+ finish({error, nocode}, Req2, Opts);
+ {Code, Req2} ->
+ get_access_token(Code, Req2, Opts)
+ end;
+
+%%
+%% catchall
+%%
+handle_request(_, Req, _) ->
+ {ok, Req2} = cowboy_req:reply(404, [], <<>>, Req),
+ {ok, Req2, undefined}.
+
+%%
+%% exchange authorization code for auth token
+%%
+get_access_token(Code, Req, Opts) ->
+ {SelfUri, Req2} = cowboy_req:host_url(Req),
+ CallbackUrl = << SelfUri/binary, (key(callback_uri, Opts))/binary >>,
+ case cowboy_request:post_for_json(token_url(), [
+ {<<"code">>, Code},
+ {<<"client_id">>, key(client_id, Opts)},
+ {<<"client_secret">>, key(client_secret, Opts)},
+ {<<"redirect_uri">>, CallbackUrl},
+ {<<"grant_type">>, <<"authorization_code">>}
+ ])
+ of
+ {ok, Auth} ->
+ get_user_profile(Auth, Req2, Opts);
+ _ ->
+ finish({error, notoken}, Req2, Opts)
+ end.
+
+%%
+%% use auth tocken to extract info from user profile
+%%
+get_user_profile(Auth, Req, Opts) ->
+ AccessToken = key(<<"access_token">>, Auth),
+ case cowboy_request:get_json(profile_url(), [
+ {<<"access_token">>, AccessToken}
+ ])
+ of
+ {ok, Profile} ->
+ finish({ok, normalize_profile(Auth, Profile)}, Req, Opts);
+ _ ->
+ finish({error, noprofile}, Req, Opts)
+ end.
+
+%%
+%% finalize application flow by calling callback handler
+%%
+finish(Status, Req, Opts) ->
+ {M, F} = key(handler, Opts),
+ M:F(Status, Req).
+
+%%
+%%------------------------------------------------------------------------------
+%% Helpers
+%%------------------------------------------------------------------------------
+%%
+
+key(Key, List) ->
+ {_, Value} = lists:keyfind(Key, 1, List),
+ Value.
+
+authorize_url() ->
+ <<"https://accounts.google.com/o/oauth2/auth">>.
+
+token_url() ->
+ <<"https://accounts.google.com/o/oauth2/token">>.
+
+profile_url() ->
+ <<"https://www.googleapis.com/oauth2/v1/userinfo">>.
+
+normalize_profile(_Auth, Raw) ->
+ [
+ {id, << "google:", (key(<<"id">>, Raw))/binary >>},
+ {provider, <<"google">>},
+ {email, key(<<"email">>, Raw)},
+ {name, key(<<"name">>, Raw)},
+ {avatar, key(<<"picture">>, Raw)},
+ {gender, key(<<"gender">>, Raw)},
+ {locale, key(<<"locale">>, Raw)}
+ ].
View
212 src/cowboy_social_providers.erl
@@ -1,106 +1,106 @@
--module(cowboy_social_providers).
--author('Vladimir Dronnikov <dronnikov@gmail.com>').
-
--export([authorize_url/1]).
--export([custom_data/3]).
--export([normalize_profile/3]).
--export([profile_url/1]).
--export([token_url/1]).
-
-%%
-%%------------------------------------------------------------------------------
-%% Helpers
-%%------------------------------------------------------------------------------
-%%
-
-key(Key, List) ->
- {_, Value} = lists:keyfind(Key, 1, List),
- Value.
-
-%%
-%%------------------------------------------------------------------------------
-%% Providers
-%%------------------------------------------------------------------------------
-%%
-
-authorize_url(google) -> <<"https://accounts.google.com/o/oauth2/auth">>;
-authorize_url(github) -> <<"https://github.com/login/oauth/authorize">>;
-authorize_url(twitter) -> <<"https://api.twitter.com/oauth/authorize">>;
-authorize_url(vkontakte) -> <<"https://oauth.vk.com/authorize">>;
-authorize_url(yandex) -> <<"https://oauth.yandex.ru/authorize">>.
-
-token_url(google) -> <<"https://accounts.google.com/o/oauth2/token">>;
-token_url(github) -> <<"https://github.com/login/oauth/access_token">>;
-token_url(twitter) -> <<"https://api.twitter.com/oauth/access_token">>;
-token_url(vkontakte) -> <<"https://oauth.vk.com/access_token">>;
-token_url(yandex) -> <<"https://oauth.yandex.ru/token">>.
-
-profile_url(google) -> <<"https://www.googleapis.com/oauth2/v1/userinfo">>;
-profile_url(github) -> <<"https://api.github.com/user">>;
-profile_url(twitter) -> <<"https://api.twitter.com/1/users/lookup.json">>;
-profile_url(vkontakte) -> <<"https://api.vk.com/method/users.get">>;
-profile_url(yandex) -> <<"https://login.yandex.ru/info">>.
-
-custom_data(twitter, AccessToken, _ProviderOpts) ->
- UserId = key(<<"user_id">>, AccessToken),
- [{<<"user_id">>, UserId}];
-custom_data(vkontakte, _AccessToken, ProviderOpts) ->
- Scope = key(scope, ProviderOpts),
- [{<<"fields">>, Scope}];
-custom_data(yandex, AccessToken, _ProviderOpts) ->
- [{<<"oauth_token">>, AccessToken}, {<<"format">>, <<"json">>}];
-custom_data(_, _, _) ->
- [].
-
-normalize_profile(google, _A, P) ->
- [
- {id, << "google:", (key(<<"id">>, P))/binary >>},
- {provider, <<"google">>},
- {email, key(<<"email">>, P)},
- {name, key(<<"name">>, P)},
- {avatar, key(<<"picture">>, P)},
- {gender, key(<<"gender">>, P)},
- {locale, key(<<"locale">>, P)}
- ];
-
-normalize_profile(github, _A, P) ->
- [
- {id, << "github:",
- (list_to_binary(integer_to_list(key(<<"id">>, P))))/binary >>},
- {provider, <<"github">>},
- {email, key(<<"email">>, P)},
- {name, key(<<"name">>, P)},
- {avatar, key(<<"avatar_url">>, P)}
- ];
-
-normalize_profile(twitter, _A, P) ->
- [
- {id, << "twitter:", (key(<<"id">>, P))/binary >>},
- {provider, <<"twitter">>},
- {name, key(<<"name">>, P)},
- {avatar, key(<<"profile_image_url_https">>, P)}
- ];
-
-normalize_profile(vkontakte, _A, P0) ->
- % NB: provider returns list of data for uids; we need only the first
- [P] = key(<<"response">>, P0),
- [
- {id, << "vkontakte:",
- (list_to_binary(integer_to_list(key(<<"uid">>, P))))/binary >>},
- {provider, <<"vkontakte">>},
- % {email, key(<<"email">>, P)},
- {name, << (key(<<"first_name">>, P))/binary, " ",
- (key(<<"last_name">>, P))/binary >>},
- {avatar, key(<<"photo">>, P)},
- {gender, case key(<<"sex">>, P) of 1 -> <<"female">>; _ -> <<"male">> end}
- ];
-
-normalize_profile(yandex, _A, P) ->
- [
- {id, << "yandex:", (key(<<"id">>, P))/binary >>},
- {provider, <<"yandex">>},
- {email, key(<<"default_email">>, P)},
- {name, key(<<"real_name">>, P)},
- % {avatar, key(<<"picture">>, P)},
- {gender, case key(<<"sex">>, P) of 1 -> <<"female">>; _ -> <<"male">> end}
- ].
+-module(cowboy_social_providers).
+-author('Vladimir Dronnikov <dronnikov@gmail.com>').
+
+-export([authorize_url/1]).
+-export([custom_data/3]).
+-export([normalize_profile/3]).
+-export([profile_url/1]).
+-export([token_url/1]).
+
+%%
+%%------------------------------------------------------------------------------
+%% Helpers
+%%------------------------------------------------------------------------------
+%%
+
+key(Key, List) ->
+ {_, Value} = lists:keyfind(Key, 1, List),
+ Value.
+
+%%
+%%------------------------------------------------------------------------------
+%% Providers
+%%------------------------------------------------------------------------------
+%%
+
+authorize_url(google) -> <<"https://accounts.google.com/o/oauth2/auth">>;
+authorize_url(github) -> <<"https://github.com/login/oauth/authorize">>;
+authorize_url(twitter) -> <<"https://api.twitter.com/oauth/authorize">>;
+authorize_url(vkontakte) -> <<"https://oauth.vk.com/authorize">>;
+authorize_url(yandex) -> <<"https://oauth.yandex.ru/authorize">>.
+
+token_url(google) -> <<"https://accounts.google.com/o/oauth2/token">>;
+token_url(github) -> <<"https://github.com/login/oauth/access_token">>;
+token_url(twitter) -> <<"https://api.twitter.com/oauth/access_token">>;
+token_url(vkontakte) -> <<"https://oauth.vk.com/access_token">>;
+token_url(yandex) -> <<"https://oauth.yandex.ru/token">>.
+
+profile_url(google) -> <<"https://www.googleapis.com/oauth2/v1/userinfo">>;
+profile_url(github) -> <<"https://api.github.com/user">>;
+profile_url(twitter) -> <<"https://api.twitter.com/1/users/lookup.json">>;
+profile_url(vkontakte) -> <<"https://api.vk.com/method/users.get">>;
+profile_url(yandex) -> <<"https://login.yandex.ru/info">>.
+
+custom_data(twitter, AccessToken, _ProviderOpts) ->
+ UserId = key(<<"user_id">>, AccessToken),
+ [{<<"user_id">>, UserId}];
+custom_data(vkontakte, _AccessToken, ProviderOpts) ->
+ Scope = key(scope, ProviderOpts),
+ [{<<"fields">>, Scope}];
+custom_data(yandex, AccessToken, _ProviderOpts) ->
+ [{<<"oauth_token">>, AccessToken}, {<<"format">>, <<"json">>}];
+custom_data(_, _, _) ->
+ [].
+
+normalize_profile(google, _A, P) ->
+ [
+ {id, << "google:", (key(<<"id">>, P))/binary >>},
+ {provider, <<"google">>},
+ {email, key(<<"email">>, P)},
+ {name, key(<<"name">>, P)},
+ {avatar, key(<<"picture">>, P)},
+ {gender, key(<<"gender">>, P)},
+ {locale, key(<<"locale">>, P)}
+ ];
+
+normalize_profile(github, _A, P) ->
+ [
+ {id, << "github:",
+ (list_to_binary(integer_to_list(key(<<"id">>, P))))/binary >>},
+ {provider, <<"github">>},
+ {email, key(<<"email">>, P)},
+ {name, key(<<"name">>, P)},
+ {avatar, key(<<"avatar_url">>, P)}
+ ];
+
+normalize_profile(twitter, _A, P) ->
+ [
+ {id, << "twitter:", (key(<<"id">>, P))/binary >>},
+ {provider, <<"twitter">>},
+ {name, key(<<"name">>, P)},
+ {avatar, key(<<"profile_image_url_https">>, P)}
+ ];
+
+normalize_profile(vkontakte, _A, P0) ->
+ % NB: provider returns list of data for uids; we need only the first
+ [P] = key(<<"response">>, P0),
+ [
+ {id, << "vkontakte:",
+ (list_to_binary(integer_to_list(key(<<"uid">>, P))))/binary >>},
+ {provider, <<"vkontakte">>},
+ % {email, key(<<"email">>, P)},
+ {name, << (key(<<"first_name">>, P))/binary, " ",
+ (key(<<"last_name">>, P))/binary >>},
+ {avatar, key(<<"photo">>, P)},
+ {gender, case key(<<"sex">>, P) of 1 -> <<"female">>; _ -> <<"male">> end}
+ ];
+
+normalize_profile(yandex, _A, P) ->
+ [
+ {id, << "yandex:", (key(<<"id">>, P))/binary >>},
+ {provider, <<"yandex">>},
+ {email, key(<<"default_email">>, P)},
+ {name, key(<<"real_name">>, P)},
+ % {avatar, key(<<"picture">>, P)},
+ {gender, case key(<<"sex">>, P) of 1 -> <<"female">>; _ -> <<"male">> end}
+ ].
Please sign in to comment.
Something went wrong with that request. Please try again.