diff --git a/apps/roster/src/protocol/micro_auth.erl b/apps/roster/src/protocol/micro_auth.erl index e97661e8f10647619b5b940441e2e44b8201ce83..3953b33e171080fc31d96803996217c753822628 100644 --- a/apps/roster/src/protocol/micro_auth.erl +++ b/apps/roster/src/protocol/micro_auth.erl @@ -20,9 +20,9 @@ description() -> "Micro Authentication Module". check(#mqtt_client{client_id = <<"sys_micro_", _/binary>> = ClientId, client_pid = ClientPid}, _Pwd, _State) -> ?LOG_INFO("~p:Auth:auth(micro)/check", [ClientId]), - emqttd_client:subscribe(ClientPid, [{roster:action_topic(ClientId), 2}]), + emqttd_client:subscribe(ClientPid, [{roster:action_topic(ClientId), 2}]), ignore; -check(#mqtt_client{client_id = <<"emqttd_", _/binary>> = ClientId, +check(#mqtt_client{client_id = <<"emqttd_", _/binary>> = ClientId, username = <<"micro">>, ws_initial_headers = [_|_] = Headers} = MC, Pwd, State) when not is_tuple(Pwd) -> ?LOG_INFO("~p:Auth:auth(micro)/check/ws headers: ~p", [ClientId, Headers]), @@ -30,10 +30,10 @@ check(#mqtt_client{client_id = <<"emqttd_", _/binary>> = ClientId, check(MC, {token, Jwt}, State); check(#mqtt_client{client_id = <<"emqttd_", _/binary>>, username = <<"micro">>} = MC, ?SYS_TOKEN=Pwd, State) -> -check(MC, {token, Pwd}, State); -check(#mqtt_client{client_id = <<"emqttd_", _/binary>> = ClientId, + check(MC, {token, Pwd}, State); +check(#mqtt_client{client_id = <<"emqttd_", _/binary>> = ClientId, username = <<"micro">>, - client_pid = ClientPid, + client_pid = ClientPid, will_topic = WT}, {token, Token}, _State) -> ?LOG_INFO("~p:Auth:auth(micro)/check", [ClientId]), case WT of @@ -51,9 +51,11 @@ check(#mqtt_client{client_id = <<"emqttd_", _/binary>> = ClientId, Auth1 = Auth0#'Auth'{last_online = roster:now_msec(), type = verified}, kvs:put(Auth1), ?LOG_INFO("~p:Auth:auth(micro)/check:session created, post logout ", [ClientId]), + roster:sub_client(subscribe, ClientId, PhoneId), AuthPid ! roster_auth:control_ver(Auth1, Ver), ok; {ok, #'Auth'{user_id = PhoneId} = Auth} -> + roster:sub_client(subscribe, ClientId, PhoneId), AuthPid ! roster_auth:control_ver(Auth#'Auth'{type = []}, Ver), ok; {ok, #'Auth'{user_id = _PhoneId2} = Auth} -> @@ -67,6 +69,7 @@ check(#mqtt_client{client_id = <<"emqttd_", _/binary>> = ClientId, kvs:put(Auth = #'Auth'{client_id = ClientId, user_id = PhoneId, phone = roster:phone(PhoneId), created = roster:now_msec(), last_online = roster:now_msec(), type = verified}), ?LOG_INFO("~p:Auth:auth(micro)/check:session created", [ClientId]), + roster:sub_client(subscribe, ClientId, PhoneId), AuthPid ! roster_auth:control_ver( Auth#'Auth'{client_id = ClientId, user_id = PhoneId, type = verified}, Ver), ok diff --git a/apps/roster/src/protocol/roster_auth.erl b/apps/roster/src/protocol/roster_auth.erl index 0c22755a789908fd08f6944cb2a02ddd3e9fd6b8..b0fad914e410eeee90639ccc94ded9589defc343 100644 --- a/apps/roster/src/protocol/roster_auth.erl +++ b/apps/roster/src/protocol/roster_auth.erl @@ -21,7 +21,7 @@ start() -> stop() -> n2o_async:stop(system, roster_auth). -init([Listeners]) -> +init([Listeners]) -> ?LOG_INFO("init/2: Listeners: ~p", [Listeners]), {ok, Listeners}. @@ -72,7 +72,7 @@ check(#mqtt_client{ token = Token, settings = Settings, last_online = LO - } = Auth) when Type == verified; + } = Auth) when Type == verified; Type == []; Type == expired -> ?LOG_INFO("[WS Headers]: ~p", [Headers]), @@ -94,9 +94,9 @@ check(#mqtt_client{ ), AuthPid = roster:n2o_pid(?MODULE), Now = roster:now_msec(), - LastOnline = case Type of + LastOnline = case Type of verified -> Now; - _ -> LO + _ -> LO end, Auth2 = Auth#'Auth'{ type = update, @@ -108,7 +108,7 @@ check(#mqtt_client{ {bad_token,_} -> Auth3 = Auth2#'Auth'{ type = expired, - token = element(2,roster:gen_token([],[])) + token = element(2,roster:gen_token([],[])) }, ?LOG_INFO("Auth:auth/roamToken:~p", [Auth3]), kvs:put(Auth3), @@ -126,7 +126,7 @@ check(#mqtt_client{ {_, _} -> OldIpBin = case FindIp = lists:keyfind(?IP_KEY, #'Feature'.key, Settings) of false -> IpBin; - #'Feature'{value = V} -> V + #'Feature'{value = V} -> V end, IsCheckIp = application:get_env(roster, auth_check_ip, true), Ver = binary_to_list(BVer), @@ -170,7 +170,7 @@ check(#mqtt_client{ ) ) of true -> []; - _ -> verified + _ -> verified end, kvs:put(Auth#'Auth'{token = Token, type = Type}), check(MqttClient, Token, Auth#'Auth'{token = Token, type = Type}); @@ -195,9 +195,9 @@ check(#mqtt_client{ {error, not_found} = Error404 -> ?LOG_ERROR("~p:Auth/check:~p", [ClientId, Error404]), Error404; - Err -> + Err -> ?LOG_ERROR("~p:Auth:auth/check:~p", [ClientId, Err]), - Err + Err end; check(#mqtt_client{ client_id = <<"emqttd_", _/binary>> = ClientId, @@ -263,7 +263,7 @@ info(#'Auth'{ {error, _} -> skip; {ok, #'Auth'{} = A} -> - roster_presence:on_disconnect(A#'Auth'{type = logout}, C) + roster_presence:on_disconnect(A#'Auth'{type = logout}, C) end; {ok, #'Auth'{client_id = ClId}} -> kvs:delete('Auth', ClId) @@ -286,7 +286,7 @@ info(#'Auth'{ {ok, _} -> Fun = case Services of [] -> generate_sms; - [jwt] -> generate_jwt + [jwt] -> generate_jwt end, {ok, Codes} = telesign_api:Fun(Phone), kvs:put(Auth#'Auth'{ @@ -431,7 +431,7 @@ info(#'Auth'{ UserId = roster:phone_id(Phone, RosterId), ClientId = client(RegClientId), [ - kvs:delete('Auth', ClId) + kvs:delete('Auth', ClId) || #'Auth'{client_id = ClId} <- kvs:index('Auth', dev_key, DevKey) ], {'Token', Token} = roster:gen_token([], []), @@ -510,7 +510,7 @@ info(#'Auth'{type = clear}, IO = #io{ code = case kvs:get('Auth', ClientId) of {ok, #'Auth'{user_id = PhoneId}} -> - [roster:force_logout(A) + [roster:force_logout(A) || #'Auth'{client_id = ClId} = A <- kvs:index('Auth', user_id, PhoneId), ClId /= ClientId ], #ok{code = cleared}; @@ -556,7 +556,7 @@ info(#'Auth'{type = get}, IO = case kvs:get('Auth', ClientId) of {ok, #'Auth'{user_id = PhoneId}} -> [ - Auth#'Auth'{token = []} + Auth#'Auth'{token = []} || #'Auth'{type = Type} = Auth <- kvs:index('Auth', user_id, PhoneId), Type == verified orelse Type == []]; {error, _} = Err -> @@ -574,10 +574,10 @@ info(#'Auth'{type = logout}, {ok, Auth} -> kvs:put(Auth#'Auth'{type = logout}); _ -> - skip + skip end, {bert, #io{code = #ok{code = logout}}}; - _ -> + _ -> <<>> end, {reply, D, Req, State}; @@ -617,7 +617,7 @@ proc(#'Auth'{ RegClientId = <<"reg_", Post/binary>>, n2o_vnode:unsubscribe(RegClientId, roster:action_topic(RegClientId)), kvs:delete(mqtt_session, RegClientId), - catch roster_presence:on_verify(ClientId, UserId), + catch roster:sub_client(subscribe, ClientId, UserId), Reply; %%proc(#'Auth'{type = [], token = Token, phone = Phone, client_id = <<"emqttd_", _/binary>> = ClientId}=Auth,H)-> %% proc(Auth#'Auth'{type = {ver,?VERSION}}, H); @@ -661,7 +661,7 @@ proc({vox, #'Profile'{phone = Phone, services = Services} = Profile, Cli}, ok -> {VoxResponse1} = VoxResponse, [Id, U, P] = [ - proplists:get_value(Key, VoxResponse1) + proplists:get_value(Key, VoxResponse1) || Key <- [user_id, user_name, user_password] ], ?LOG_INFO("~p:~p:vox:~p:~p:~p", [Phone, Cli, Id, U, P]), @@ -673,7 +673,7 @@ proc({vox, #'Profile'{phone = Phone, services = Services} = Profile, Cli}, status = added }; _ER -> - [] + [] end, kvs:put(Profile2 = #'Profile'{rosters = Rosters} = Profile#'Profile'{services = lists:flatten([Service | Services])}), case emqttd_cm:lookup(Cli) of @@ -710,15 +710,15 @@ proc({voice, Phone, SmsCode, Lang}, #handler{} = H) -> {reply, [], H}; proc({mqttc, C, connected}, - State = #handler{state = C, seq = S}) -> + State = #handler{state = C, seq = S}) -> {ok, State#handler{seq = S + 1}}; -proc({mqttc, _C, disconnected}, State) -> +proc({mqttc, _C, disconnected}, State) -> {ok, State}; proc(Unknown, #handler{} = H) -> ?LOG_INFO("invalid auth data :~p", [Unknown]), {reply, [], H}. -client(<<"reg_", Post/binary>>) -> +client(<<"reg_", Post/binary>>) -> <<"emqttd_", Post/binary>>; client(ClientId) -> ClientId. @@ -756,12 +756,12 @@ get_geolocal(IP) -> V when is_binary(V) -> {K, V}; _ -> - [] - end + [] + end || {K, Keys} <- L ]); {error, _} -> - [] + [] end. geoval([], Acc) -> Acc; @@ -781,7 +781,7 @@ delete_duplicated_tokens(ClientId, PushToken) -> _ -> ?LOG_INFO("DeleteOldAuthSession:~p:~p", [PushToken, SessionId]), kvs:delete('Auth', SessionId) - end + end || #'Auth'{client_id = SessionId} <- kvs:index('Auth', push, PushToken) ]. @@ -803,6 +803,6 @@ control_ver(A, Ver) -> A#'Auth'{type = {ver, Ver}}; verified -> A#'Auth'{type = {reg, Ver}}; - _ -> + _ -> A end. diff --git a/apps/roster/src/protocol/roster_presence.erl b/apps/roster/src/protocol/roster_presence.erl index b512d067e27f3a43ff7f3a2303b5acc4c129783c..1b169ab050a0481a5ca63935f815e7b576da2a64 100644 --- a/apps/roster/src/protocol/roster_presence.erl +++ b/apps/roster/src/protocol/roster_presence.erl @@ -43,7 +43,7 @@ on_disconnect(#'Auth'{phone = Phone, client_id = ClientId, type=Type}, C) -> ?LOG_INFO("~p:~p:DISCONNECT:~p", [Phone, ClientId, Type]), send_presence(offline, Phone, C, ClientId). -on_verify(ClientId, PhoneId) -> roster:sub_client(subscribe, ClientId, PhoneId). +on_verify(_ClientId, _PhoneId) -> ok. %% Warning: Do not always delete Auth record, it holds push notification tokens should_preserve_auth_record(ClientId) ->