-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnonce.erl
88 lines (61 loc) · 1.89 KB
/
nonce.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
%
% Copyright (c) 2008, 2009, 2010 JackNyfe, Inc. (dba Echo) http://aboutecho.com/
% See the accompanying LICENSE file.
%
%
% Nonce server
%
-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(),
Nonce = "jskitnonce" ++ "-" ++ integer_to_list(erlang:phash2(make_ref())),
{reply, {Ts, Nonce}, {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.