5656% % IQ handler
5757-export ([iq_handler /1 ]).
5858
59- % % Exports for XMPP-generated code
60- -export ([
61- do_decode /4 ,
62- tags /0 ,
63- do_encode /2 ,
64- do_get_name /1 ,
65- do_get_ns /1 ,
66- pp /2 ,
67- records /0 ,
68- decode_unified_push_register /3 ,
69- decode_unified_push_register_attrs /4 ,
70- encode_unified_push_register /2 ,
71- decode_unified_push_register_attr_application /2 ,
72- decode_unified_push_register_attr_instance /2
73- ]).
59+ -type ttl_timeout () :: undefined | non_neg_integer ().
7460
7561% % API subpaths
7662-define (ENDPOINT_PUSH , " push" ).
8470-define (UP_INSTANCE , <<" i" >>).
8571
8672% % called by ejabberd_http
87- -spec process ([binary ()], http_request ()) -> {integer (), [{binary (), binary ()}], binary ()}.
73+ -spec process ([binary ()], http_request ()) -> {integer (), [{binary (), binary ()}], [ binary ()] }.
8874process (_LocalPath , # request {method = 'POST' , data = <<>>}) ->
8975 ? DEBUG (" bad POST request for ~p : no data" , [_LocalPath ]),
9076 {400 , [], []};
@@ -99,28 +85,28 @@ process([], #request{method = 'GET'}) ->
9985 [
10086 {<<" Content-Type" >>, <<" application/json" >>}
10187 ],
102- [" {\" unifiedpush\" :{\" version\" :1}}" ]};
88+ [<< " {\" unifiedpush\" :{\" version\" :1}}" >> ]};
10389process ([<<? ENDPOINT_MESSAGE >> | _ ], _Request ) ->
10490 % % RFC8030: 6.2.
10591 {410 , [], []};
10692process (_LocalPath , _Request ) ->
10793 ? DEBUG (" bad request for ~p : ~p " , [_LocalPath , _Request ]),
10894 {400 , [], []}.
10995
110- -spec get_ttl ([{atom (), binary ()}]) -> integer ().
96+ -spec get_ttl ([{atom (), binary ()}]) -> ttl_timeout ().
11197get_ttl (Headers ) ->
11298 try string :to_integer (proplists :get_value (<<" Ttl" >>, Headers , " 1" )) of
113- {T , <<>>} -> T ;
114- {_ , _ } -> - 1
99+ {T , <<>>} when T >= 0 -> T ;
100+ {_ , _ } -> undefined
115101 catch
116102 _ :_ ->
117103 % % assume illegal behavior
118- - 1
104+ undefined
119105 end .
120106
121- -spec validate_request (binary (), binary (), binary (), binary (), integer ()) ->
122- {integer (), [{binary (), binary ()}], binary () }.
123- validate_request (_Host , _Jwk , _MaybeJwtToken , _Data , Ttl ) when Ttl < 0 ->
107+ -spec validate_request (binary (), any (), binary (), binary (), ttl_timeout ()) ->
108+ {integer (), [{binary (), binary ()}], [] }.
109+ validate_request (_Host , _Jwk , _MaybeJwtToken , _Data , undefined ) ->
124110 {400 , [], []};
125111validate_request (Host , Jwk , MaybeJwtToken , Data , Ttl ) when Data =/= <<" " >> ->
126112 ? DEBUG (" verifying jwt validity" , []),
@@ -156,8 +142,8 @@ validate_request(Host, Jwk, MaybeJwtToken, Data, Ttl) when Data =/= <<"">> ->
156142validate_request (_Host , _Jwk , _MaybeJwtToken , _Data , _Ttl ) ->
157143 {400 , [], []}.
158144
159- -spec forward_push_message (binary (), binary (), binary (), integer (), map ()) ->
160- {integer (), [{binary (), binary ()}], binary () }.
145+ -spec forward_push_message (binary (), binary (), binary (), ttl_timeout (), map ()) ->
146+ {integer (), [{binary (), binary ()}], [] }.
161147forward_push_message (Host , Jwt , Data , Ttl , #{
162148 ? UP_APPLICATION := Application , ? UP_INSTANCE := Instance , ? UP_OWNER := To
163149}) ->
@@ -179,6 +165,7 @@ forward_push_message(Host, Jwt, Data, Ttl, #{
179165 ? DEBUG (" ~p : IQ callback: ~p " , [? MODULE , _Res ]),
180166 ok
181167 end ,
168+ undefined ,
182169 Ttl
183170 ),
184171 UrlPrefix = misc :expand_keyword (<<" @HOST@" >>, guess_url_prefix (any ), Host ),
@@ -250,7 +237,7 @@ get_jwk(Host) ->
250237- spec mod_opt_type (atom ()) -> econf :validator ().
251238mod_opt_type (expiration ) ->
252239 % % TODO is there an upper bound for the validity of the JWT token?
253- econf :int (0 , 60 * 60 * 24 );
240+ econf :int (0 , 86400 ); % % 60 * 60 * 24
254241mod_opt_type (jwk ) ->
255242 econf :map (econf :binary (), econf :either (econf :binary (), econf :int ())).
256243
@@ -334,20 +321,3 @@ mod_doc() ->
334321 " jwk: {\" k\" => \" a4-...\" ,\" kty\" => \" oct\" }"
335322 ]
336323 }.
337-
338- % % FIXME The following lines add support for the custom IQ stanza.
339- % % If possible they should be moved to the xmpp package.
340- % % Created automatically by XML generator (fxml_gen.erl)
341- % % Source: xmpp_codec.spec
342-
343- do_decode (
344- <<" push" >>,
345- <<" http://gultsch.de/xmpp/drafts/unified-push" >>,
346- El ,
347- Opts
348- ) ->
349- decode_unified_push_push (
350- <<" http://gultsch.de/xmpp/drafts/unified-push" >>,
351- Opts ,
352- El
353- );
0 commit comments