Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/simple/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.rebar
_rel
15 changes: 15 additions & 0 deletions examples/simple/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
REBAR = rebar
RELX = relx

.PHONY: all deps compile rel

all: rel

deps:
$(REBAR) get-deps

compile: deps
$(REBAR) compile

rel: compile
$(RELX)
36 changes: 36 additions & 0 deletions examples/simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Simple Social Example

This example shows the basic usage of social for OAuth.

By default, the HTTP server listens on port 8080.

## Configuration

In `etc/app.config` there is a commented list of OAuth provider
configs. To get the example working, uncomment the desired sections
and fill in the client id, secret, and any other necessary values. You
should be able to get these values from the OAuth provider.

If no OAuth providers are configured you will be unable to use social.

## Usage

To build and run the application:

```shell
# fetch the deps, compile the app, and build the release
make

# start the application in the foreground
./_rel/simple_example/bin/simple_example foreground
```

You should now be able to login into a configured OAuth provider at
`auth/$provider/login`.

## Environment

This was tested using:

- rebar 2.5.1-1-ge9f62c4
- relx 1.1.0 (there were issues with 1.2.0)
63 changes: 63 additions & 0 deletions examples/simple/etc/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[{simple, [{port, 8080},
{oauth_providers,
[
%% {<<"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">>}
%% ]},
%% {<<"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">>}
%% ]},
%% {<<"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">>}
%% ]}
]}
]}].
1 change: 1 addition & 0 deletions examples/simple/etc/sys.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["etc/app.config"].
12 changes: 12 additions & 0 deletions examples/simple/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{erl_opts, [
debug_info,
warn_format,
warn_export_vars,
warn_obsolete_guard,
warn_bif_clash
]}.

{deps, [
{cowboy, ".*", {git, "https://github.com/ninenines/cowboy.git", {tag, "1.0.1"}}},
{social, ".*", {git, "https://github.com/dvv/social.git", {branch, "master"}}}
]}.
4 changes: 4 additions & 0 deletions examples/simple/relx.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{release, {simple_example, "1"}, [simple]}.
{extended_start_script, true}.
{sys_config, "etc/sys.config"}.
{overlay, [{copy, "etc/app.config", "etc/app.config"}]}.
13 changes: 13 additions & 0 deletions examples/simple/src/simple.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{application, simple,
[
{description, ""},
{vsn, "1"},
{registered, []},
{applications, [
kernel,
stdlib,
social
]},
{mod, { simple_app, []}},
{env, []}
]}.
33 changes: 33 additions & 0 deletions examples/simple/src/simple_app.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-module(simple_app).

-behaviour(application).

%% Application callbacks
-export([start/2, stop/1]).

-define(HTTP_PORT, 8080).
-define(COWBOY_REF, cowboy).

%% ===================================================================
%% Application callbacks
%% ===================================================================

start(_StartType, _StartArgs) ->
Dispatch = cowboy_router:compile([{'_', routes()}]),
{ok, _} = cowboy:start_http(?COWBOY_REF, 100,
[{port, ?HTTP_PORT}],
[{env, [{dispatch, Dispatch}]}]),
simple_sup:start_link().

stop(_State) ->
cowboy:stop_lister(?COWBOY_REF).

%% ===================================================================
%% Internal
%% ===================================================================

routes() ->
case application:get_env(oauth_providers) of
{ok, OAuthProviders} ->
[{"/auth/:provider/:action", cowboy_social, OAuthProviders}]
end.
26 changes: 26 additions & 0 deletions examples/simple/src/simple_sup.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-module(simple_sup).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).

%% ===================================================================
%% API functions
%% ===================================================================

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init([]) ->
{ok, {{one_for_one, 5, 10}, []}}.
3 changes: 2 additions & 1 deletion src/social.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
kernel,
stdlib,
ssl,
cowboy
cowboy,
jsx
]},
{env, []}
]}.