Skip to content

Commit

Permalink
Initial import of the OAuth client from Jacknyfe hg repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Skvortsov committed Jan 4, 2010
1 parent 9363a25 commit 8601b9c
Show file tree
Hide file tree
Showing 13 changed files with 1,523 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*~
*.beam
oauth.app
40 changes: 20 additions & 20 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
Copyright (c) 2007, 2008, 2009 JackNyfe, Inc. <info@jacknyfe.net>.
Copyright (c) 2008-2009 Jacknyfe, Inc., http://jacknyfe.net.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
45 changes: 34 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
PROJECTNAME=mcd
PROJECTVERSION=1.0
YAWS_EBIN = /local/lib/yaws/ebin

INSTALLDIR=$(prefix)/$(LIBDIR)/$(PROJECTNAME)-$(PROJECTVERSION)/ebin
LIBDIR=$(shell erl -eval 'io:format("~s~n", [code:lib_dir()])' -s init stop -noshell)
ERLCFLAGS = -W2 -I/local/lib

all:
mkdir -p ebin
for srcfile in src/*.erl; do erlc -o ebin $$srcfile; done
EMODS_COMMON = \
error_monad \
oauth

EMODS_CLIENT = \
nonce \
oauth_app \
oauth_supervisor \
oauthclient

ESRCS_CLIENT = ${EMODS_CLIENT:%=%.erl}
ESRCS_COMMON = ${EMODS_COMMON:%=%.erl}
EOBJS_CLIENT = ${ESRCS_CLIENT:.erl=.beam}
EOBJS_COMMON = ${ESRCS_COMMON:.erl=.beam}

EOBJS = ${EOBJS_COMMON} ${EOBJS_CLIENT}

ALL_OBJS = oauth.app ${EOBJS} test.beam

all: ${ALL_OBJS}

HARDWIRE_MODULES = perl -pe 's@MODULES@join(", ", split(/\s+/, $$ENV{MODULES}))@e;'

oauth.app: oauth.app.in
MODULES="${EMODS_COMMON} ${EMODS_CLIENT}" \
${HARDWIRE_MODULES} \
< oauth.app.in > oauth.app

clean:
rm -rf ebin
rm -f ${ALL_OBJS}

.SUFFIXES: .erl .beam

install:
mkdir -p $(INSTALLDIR)
for f in ebin/*.beam; do install $$f $(INSTALLDIR); done
.erl.beam:
erlc $(ERLCFLAGS) -o . $<
7 changes: 7 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Erlang implementation of OAuth client by JS-Kit

Copyright (c) 2008-2009 Jacknyfe, Inc. All rights reserved.

This client is interoperable with OAuth 1.0 and 1.0a servers.

For documentation and samples please check out oauthclient.erl and test.erl.
8 changes: 8 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
S: support RSA-SHA1
S: WWW-Authenticate when needed
S: handle Authorization: header
S: when signature check fails, provide more diagnostics.
S: test with SSL
S: the supervisor modules should be a callback as well
C: google claims "no oauth parameters" when making a 'post' request with
get_request_token.
51 changes: 51 additions & 0 deletions error_monad.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
%
% Copyright (c) 2008-2009 Jacknyfe, Inc., http://jacknyfe.net.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% * Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%

-module(error_monad).
-export([
do/1,
do/2,
error/1,
error/2,
error/3
]).

% Poor man's Error monad.
do(F) -> do(F, undefined).

do([], Arg) -> Arg;

do([Fun | RestFuns], Arg) ->
case Fun(Arg) of
{error, Error} -> Error;
V -> do(RestFuns, V)
end.

% "Fail" functions. The application will see {error, A, B, ...} value extracted
% out of the monad.
error(A) -> {error, {error, A}}.
error(A, B) -> {error, {error, A, B}}.
error(A, B, C) -> {error, {error, A, B, C}}.
106 changes: 106 additions & 0 deletions nonce.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
%
% Nonce server
%
% Copyright (c) 2008-2009 Jacknyfe, Inc., http://jacknyfe.net.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% * Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%

-module(nonce).
-behaviour(gen_server).

-export([
% public API
get_ts_nonce/1,
start_link/0,

% gen_server callbacks
code_change/3,
handle_call/3,
handle_cast/2,
handle_info/2,
init/1,
terminate/2
]).

-define(RESET_PERIOD, 5000).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Public API
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

get_ts_nonce(ServerRef) ->
gen_server:call(ServerRef, {get_ts_nonce}).


start_link() ->
gen_server:start_link({local, oauth_nonce}, ?MODULE, args, []).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% gen_server callbacks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

code_change(_OldVsn, State, _Extra) ->
{ok, State}.


handle_call({get_ts_nonce}, _From, {_LastTs, N}) ->
Ts = get_ts(),
{reply, {Ts, "jskitnonce" ++ integer_to_list(N)}, {Ts, N + 1}};

handle_call(_Request, _From, State) ->
{noreply, State}.


handle_cast(_Request, State) ->
{noreply, State}.


handle_info({reset}, {LastTs, _N} = State) ->
Ts = get_ts(),
if
% We saw last request this very second, so it's not yet safe to reset
% Nonce value (otherwise there's a chance it won't be unique).
Ts == LastTs -> {noreply, State};

true -> {noreply, {LastTs, 0}}
end.


init(_Args) ->
timer:send_interval(?RESET_PERIOD, {reset}),
{ok, {0, 0}}.


terminate(_Reason, _State) ->
ok.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Internal functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

get_ts() ->
{MegaSecs, Secs, _} = now(),
MegaSecs * 1000000 + Secs.
9 changes: 9 additions & 0 deletions oauth.app.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{application, oauth,
[ {description, "OAuth implementation"},
{vsn, "0.1"},
{modules, [ MODULES ] },
{registered, [oauth_nonce]},
{applications, [kernel, stdlib, sasl]},
{mod, {oauth_app, []}}
]
}.
Loading

0 comments on commit 8601b9c

Please sign in to comment.