diff --git a/examples/simple/.gitignore b/examples/simple/.gitignore new file mode 100644 index 0000000..c3adbf0 --- /dev/null +++ b/examples/simple/.gitignore @@ -0,0 +1,2 @@ +.rebar +_rel \ No newline at end of file diff --git a/examples/simple/Makefile b/examples/simple/Makefile new file mode 100644 index 0000000..557afc7 --- /dev/null +++ b/examples/simple/Makefile @@ -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) diff --git a/examples/simple/README.md b/examples/simple/README.md new file mode 100644 index 0000000..8238bf5 --- /dev/null +++ b/examples/simple/README.md @@ -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) diff --git a/examples/simple/etc/app.config b/examples/simple/etc/app.config new file mode 100644 index 0000000..84b2f67 --- /dev/null +++ b/examples/simple/etc/app.config @@ -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">>} + %% ]} + ]} + ]}]. diff --git a/examples/simple/etc/sys.config b/examples/simple/etc/sys.config new file mode 100644 index 0000000..b036be6 --- /dev/null +++ b/examples/simple/etc/sys.config @@ -0,0 +1 @@ +["etc/app.config"]. \ No newline at end of file diff --git a/examples/simple/rebar.config b/examples/simple/rebar.config new file mode 100644 index 0000000..8f0a5ea --- /dev/null +++ b/examples/simple/rebar.config @@ -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"}}} +]}. diff --git a/examples/simple/relx.config b/examples/simple/relx.config new file mode 100644 index 0000000..1fb9a56 --- /dev/null +++ b/examples/simple/relx.config @@ -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"}]}. \ No newline at end of file diff --git a/examples/simple/src/simple.app.src b/examples/simple/src/simple.app.src new file mode 100644 index 0000000..772fa25 --- /dev/null +++ b/examples/simple/src/simple.app.src @@ -0,0 +1,13 @@ +{application, simple, + [ + {description, ""}, + {vsn, "1"}, + {registered, []}, + {applications, [ + kernel, + stdlib, + social + ]}, + {mod, { simple_app, []}}, + {env, []} + ]}. diff --git a/examples/simple/src/simple_app.erl b/examples/simple/src/simple_app.erl new file mode 100644 index 0000000..5255827 --- /dev/null +++ b/examples/simple/src/simple_app.erl @@ -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. diff --git a/examples/simple/src/simple_sup.erl b/examples/simple/src/simple_sup.erl new file mode 100644 index 0000000..d39ffb3 --- /dev/null +++ b/examples/simple/src/simple_sup.erl @@ -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}, []}}. diff --git a/src/social.app.src b/src/social.app.src index a1d7989..0ab0783 100644 --- a/src/social.app.src +++ b/src/social.app.src @@ -7,7 +7,8 @@ kernel, stdlib, ssl, - cowboy + cowboy, + jsx ]}, {env, []} ]}.