1 |
|
-module(mod_auth_token_api). |
2 |
|
|
3 |
|
-include("jlib.hrl"). |
4 |
|
-include("mod_auth_token.hrl"). |
5 |
|
-include("mongoose.hrl"). |
6 |
|
|
7 |
|
-export([revoke_token_command/1, create_token/1]). |
8 |
|
|
9 |
|
-import(mod_auth_token, [token/3, serialize/1]). |
10 |
|
|
11 |
|
-spec revoke_token_command(User) -> Result when |
12 |
|
User :: jid:jid(), |
13 |
|
Result :: {ok, string()} | {not_found | internal_server_error, string()}. |
14 |
|
revoke_token_command(User) -> |
15 |
13 |
#jid{lserver = LServer} = User, |
16 |
13 |
case mongoose_domain_api:get_domain_host_type(LServer) of |
17 |
|
{ok, HostType} -> |
18 |
11 |
try mod_auth_token:revoke(HostType, User) of |
19 |
|
not_found -> |
20 |
4 |
{not_found, "User or token not found"}; |
21 |
|
ok -> |
22 |
7 |
{ok, "Revoked"}; |
23 |
|
error -> |
24 |
:-( |
{internal_server_error, "Internal server error"} |
25 |
|
catch Class:Reason:Stacktrace -> |
26 |
:-( |
?LOG_ERROR(#{what => auth_token_revoke_failed, |
27 |
:-( |
class => Class, reason => Reason, stacktrace => Stacktrace}), |
28 |
:-( |
{internal_server_error, "Internal server error"} |
29 |
|
end; |
30 |
|
_ -> |
31 |
2 |
{not_found, "Unknown domain"} |
32 |
|
end. |
33 |
|
|
34 |
|
-spec create_token(User) -> Result when |
35 |
|
User :: jid:jid(), |
36 |
|
Result :: {ok, #{binary() => string()}} | {not_found | internal_server_error, string()}. |
37 |
|
create_token(User) -> |
38 |
16 |
#jid{lserver = LServer} = User, |
39 |
16 |
case mongoose_domain_api:get_domain_host_type(LServer) of |
40 |
|
{ok, HostType} -> |
41 |
14 |
case {token(HostType, User, access), token(HostType, User, refresh)} of |
42 |
|
{#token{} = AccessToken, #token{} = RefreshToken} -> |
43 |
14 |
{ok, #{<<"access">> => serialize(AccessToken), |
44 |
|
<<"refresh">> => serialize(RefreshToken)}}; |
45 |
:-( |
_ -> {internal_server_error, "Internal server error"} |
46 |
|
end; |
47 |
|
_ -> |
48 |
2 |
{not_found, "Unknown domain"} |
49 |
|
end. |