Skip to content

Commit 6ed7d59

Browse files
committed
Merge provider redirect handlers into one module
1 parent 6ab989a commit 6ed7d59

File tree

5 files changed

+39
-53
lines changed

5 files changed

+39
-53
lines changed

src/handlers/github_redirect_handler.erl

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/handlers/google_redirect_handler.erl

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-module(provider_redirect_handler).
2+
3+
%%% HTTP handler that redirects the user to the OAuth endpoint of the corresponding
4+
%%% provider with the client_id that identifies this application
5+
6+
-export([init/3,
7+
handle/2,
8+
terminate/3]).
9+
10+
init(_Type, Req, []) ->
11+
{ok, Req, no_state}.
12+
13+
handle(Req, State) ->
14+
{Provider, _} = cowboy_req:binding(provider, Req),
15+
{EnvVar, ProviderUrl} = provider_vars(binary_to_list(Provider)),
16+
17+
case os:getenv(EnvVar, false) of
18+
false ->
19+
lager:error([Provider, <<" credentials not found in environment">>]);
20+
ClientId ->
21+
Url = [ProviderUrl | ClientId],
22+
{ok, Req2} = cowboy_req:reply(303, [{<<"location">>, Url}], Req),
23+
{ok, Req2, State}
24+
end.
25+
26+
terminate(_Reason, _Req, _State) ->
27+
ok.
28+
29+
provider_vars("google") ->
30+
{"GOOGLE_CLIENTID",
31+
[<<"https://accounts.google.com/o/oauth2/auth">>,
32+
<<"?response_type=code&redirect_uri=https://holidaypinger.com/oauth/google/callback&scope=email profile&client_id=">>]};
33+
34+
provider_vars("github") ->
35+
{"GITHUB_CLIENTID",
36+
[<<"http://github.com/login/oauth/authorize">>,
37+
<<"?scope=user:email&client_id=">>]}.

src/holiday_ping_app.erl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ start(_StartType, _StartArgs) ->
1313
{"/js/[...]", cowboy_static, {priv_dir, holiday_ping, "/ui/resources/public/js"}},
1414
{"/css/[...]", cowboy_static, {priv_dir, holiday_ping, "/ui/resources/public/css"}},
1515
{"/img/[...]", cowboy_static, {priv_dir, holiday_ping, "/ui/resources/public/img"}},
16-
{"/oauth/google", google_redirect_handler, []},
16+
{"/oauth/:provider", provider_redirect_handler, []},
1717
{"/api/auth/google/code", google_callback_handler, []},
18-
{"/oauth/github", github_redirect_handler, []},
1918
{"/api/auth/github/code", github_callback_handler, []},
2019
{"/api/users", user_handler, []},
2120
{"/api/users/confirmation", confirmation_handler, []},

src/hp_auth.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ build_holiday_access_token(Email, Name, AvatarUrl) ->
6464
name => Name,
6565
avatar => AvatarUrl
6666
},
67-
{ok, Token} = hp_auth:token_encode(Data),
67+
{ok, Token} = token_encode(Data),
6868
Token.

0 commit comments

Comments
 (0)