55% % API
66
77-export ([is_blacklisted /2 ]).
8+ -export ([is_user_blacklisted /2 ]).
89
910% % Supervisor callbacks
1011
2324% %
2425
2526-define (TAB , ? MODULE ).
27+ -define (USER_TAB , user_blacklist ).
2628
2729% %
2830
@@ -34,50 +36,58 @@ child_spec(Options) ->
3436 type => supervisor
3537 }.
3638
37- -spec is_blacklisted (tk_token :token_id (), tk_authdata :authority_id ()) -> boolean ().
39+ -spec is_blacklisted (tk_token :token_id (), tk_token :authority_id ()) -> boolean ().
3840is_blacklisted (TokenID , AuthorityID ) ->
39- check_entry ({AuthorityID , TokenID }).
41+ check_entry (? TAB , {AuthorityID , TokenID }).
42+
43+ -spec is_user_blacklisted (binary (), tk_token :authority_id ()) -> boolean ().
44+ is_user_blacklisted (UserID , AuthorityID ) ->
45+ check_entry (? USER_TAB , {AuthorityID , UserID }).
4046
4147% %
4248
4349-spec init (options ()) -> {ok , {supervisor :sup_flags (), [supervisor :child_spec ()]}}.
4450init (Options ) ->
45- _ = init_tab (),
51+ _ = init_tab (? TAB ),
52+ _ = init_tab (? USER_TAB ),
4653 _ = load_blacklist_conf (maps :get (path , Options , undefined )),
4754 {ok , {#{}, []}}.
4855
49- init_tab () ->
50- ets :new (? TAB , [set , protected , named_table , {read_concurrency , true }]).
56+ init_tab (Name ) ->
57+ ets :new (Name , [set , protected , named_table , {read_concurrency , true }]).
5158
5259-define (ENTRIES_KEY , " entries" ).
60+ -define (USER_ENTRIES_KEY , " user_entries" ).
5361
5462load_blacklist_conf (undefined ) ->
5563 _ = logger :warning (" No token blacklist file specified! Blacklisting functionality will be disabled." ),
5664 ok ;
5765load_blacklist_conf (Filename ) ->
5866 [Mappings ] = yamerl_constr :file (Filename ),
5967 Entries = process_entries (proplists :get_value (? ENTRIES_KEY , Mappings )),
60- put_entires (Entries ).
68+ put_entires (? TAB , Entries ),
69+ UserEntries = process_entries (proplists :get_value (? USER_ENTRIES_KEY , Mappings )),
70+ put_entires (? USER_TAB , UserEntries ).
6171
6272process_entries (Entries ) ->
6373 lists :foldl (
64- fun ({AuthorityID , TokenIDs }, Acc ) ->
65- Acc ++ [make_ets_entry (AuthorityID , ID ) || ID <- TokenIDs ]
74+ fun ({AuthorityID , IDs }, Acc ) ->
75+ Acc ++ [make_ets_entry (AuthorityID , ID ) || ID <- IDs ]
6676 end ,
6777 [],
6878 Entries
6979 ).
7080
71- make_ets_entry (AuthorityID , TokenID ) ->
72- {{list_to_binary (AuthorityID ), list_to_binary (TokenID )}, true }.
81+ make_ets_entry (AuthorityID , ID ) ->
82+ {{list_to_binary (AuthorityID ), list_to_binary (ID )}, true }.
7383
7484% %
7585
76- put_entires (Entries ) ->
77- ets :insert_new (? TAB , Entries ).
86+ put_entires (Name , Entries ) ->
87+ ets :insert_new (Name , Entries ).
7888
79- check_entry (Key ) ->
80- case ets :lookup (? TAB , Key ) of
89+ check_entry (Name , Key ) ->
90+ case ets :lookup (Name , Key ) of
8191 [_Entry ] -> true ;
8292 [] -> false
8393 end .
0 commit comments