Skip to content

Commit

Permalink
Make token generation pluggable
Browse files Browse the repository at this point in the history
This should fix issue #6 . To change how tokens are generated you can
specify in your app.config a module that should handle the token
generation. The config looks like:

```erlang

    [
        {oauth2, [
            {token_generation, YOUR_TOKEN_GENERATOR}
        ]}
    ].

```

The eefault token generator is called `oauth2_token`. To implement your
own you should create your own module exporting one function
`generate/0`.
  • Loading branch information
bipthelin committed Mar 25, 2013
1 parent 0b7edbd commit 9501d50
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/oauth2.erl
Expand Up @@ -373,7 +373,7 @@ issue_token_and_refresh(Identity, ResOwner, Scope, TTL) ->
Scope :: scope(),
TTL :: non_neg_integer().
issue_token(Identity, ResOwner, Scope, TTL) ->
AccessToken = oauth2_token:generate(),
AccessToken = oauth2_token_generation:generate(),

This comment has been minimized.

Copy link
@dvv

dvv Mar 28, 2013

Oh. generate() should be given Context so that it knows how to encode it into the token.

ExpiryAbsolute = seconds_since_epoch(TTL),
Context = build_context(Identity, ExpiryAbsolute, ResOwner, Scope),
ok = oauth2_backend:associate_access_token(AccessToken, Context),
Expand Down
7 changes: 7 additions & 0 deletions src/oauth2_config.erl
Expand Up @@ -31,6 +31,7 @@
expiry_time/0
,expiry_time/1
,backend/0
,token_generation/0
]).

%% Default time in seconds before an authentication token expires.
Expand Down Expand Up @@ -66,6 +67,12 @@ expiry_time(Flow) ->
backend() ->
get_required(backend).


%% @doc Gets the backend for generating tokens.
-spec token_generation() -> Module :: atom().
token_generation() ->
get_optional(token_generation, oauth2_token).

%%%===================================================================
%%% Internal functions
%%%===================================================================
Expand Down
41 changes: 41 additions & 0 deletions src/oauth2_token_generation.erl
@@ -0,0 +1,41 @@
%% ----------------------------------------------------------------------------
%%
%% oauth2: Erlang OAuth 2.0 implementation
%%
%% Copyright (c) 2012 KIVRA
%%
%% Permission is hereby granted, free of charge, to any person obtaining a
%% copy of this software and associated documentation files (the "Software"),
%% to deal in the Software without restriction, including without limitation
%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
%% and/or sell copies of the Software, and to permit persons to whom the
%% Software is furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
%% DEALINGS IN THE SOFTWARE.
%%
%% ----------------------------------------------------------------------------

-module(oauth2_token_generation).

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

-define(GENERATOR, (oauth2_config:token_generation())).

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

%% @doc Generates a random OAuth2 token.
-spec generate() -> Token :: oauth2:token().
generate() ->
?GENERATOR:generate().

0 comments on commit 9501d50

Please sign in to comment.