1 package com.netease.nimlib.sdk.auth;
3 import android.os.Parcel;
4 import android.os.Parcelable;
5 import android.text.TextUtils;
7 import androidx.annotation.NonNull;
14 import org.json.JSONException;
15 import org.json.JSONObject;
17 import java.io.Serializable;
18 import java.util.Objects;
23 public class LoginInfo implements Serializable, Parcelable {
24 private static final String KEY_ACCOUNT =
"KEY_ACCOUNT";
25 private static final String KEY_TOKEN =
"KEY_TOKEN";
26 private static final String KEY_AUTH_TYPE =
"KEY_AUTH_TYPE";
27 private static final String KEY_LOGIN_EXT =
"KEY_LOGIN_EXT";
28 private static final String KEY_APP_KEY =
"KEY_APP_KEY";
29 private static final String KEY_CUSTOM_CLIENT_TYPE =
"KEY_CUSTOM_CLIENT_TYPE";
32 private final String account;
34 private final String token;
38 private String loginExt;
40 private String appKey;
42 private int customClientType;
63 this.account = account == null ? null : account.toLowerCase();
89 public LoginInfo(String account, String token, String appKey) {
122 public LoginInfo(String account, String token, String appKey,
int customClientType) {
123 this(account, token, appKey);
124 this.customClientType = customClientType;
133 account = in.readString();
134 token = in.readString();
136 authType = in.readInt();
137 loginExt = in.readString();
139 appKey = in.readString();
140 customClientType = in.readInt();
143 public static final Creator<LoginInfo>
CREATOR =
new Creator<LoginInfo>() {
145 public LoginInfo createFromParcel(Parcel in) {
197 return customClientType;
206 if (authType == IAuthService.AuthType.DEFAULT) {
207 return !TextUtils.isEmpty(account) && !TextUtils.isEmpty(token);
209 return !TextUtils.isEmpty(account);
215 if (
this == o)
return true;
216 if (o == null || getClass() != o.getClass())
return false;
218 return authType == loginInfo.authType &&
219 customClientType == loginInfo.customClientType &&
220 Objects.equals(account, loginInfo.account) &&
221 Objects.equals(token, loginInfo.token) &&
222 Objects.equals(loginExt, loginInfo.loginExt) &&
223 Objects.equals(appKey, loginInfo.appKey);
230 return "LoginInfo{" +
231 "account='" + account +
'\'' +
232 ", authType=" + authType +
233 ", customClientType=" + customClientType +
239 return Objects.hash(account, token, authType, loginExt, appKey, customClientType);
249 dest.writeString(account);
250 dest.writeString(token);
252 dest.writeInt(authType);
253 dest.writeString(loginExt);
255 dest.writeString(appKey);
256 dest.writeInt(customClientType);
260 JSONObject
object =
new JSONObject();
262 object.putOpt(KEY_ACCOUNT, account);
263 object.putOpt(KEY_TOKEN, token);
264 object.putOpt(KEY_AUTH_TYPE, authType);
265 object.putOpt(KEY_LOGIN_EXT, loginExt);
266 object.putOpt(KEY_APP_KEY, appKey);
267 object.putOpt(KEY_CUSTOM_CLIENT_TYPE, customClientType);
268 }
catch (JSONException e) {
275 if (
object == null) {
279 final String account =
object.optString(KEY_ACCOUNT, null);
280 final String token =
object.optString(KEY_TOKEN, null);
281 final int authType =
object.optInt(KEY_AUTH_TYPE);
282 final String loginExt =
object.optString(KEY_LOGIN_EXT, null);
283 final String appKey =
object.optString(KEY_APP_KEY, null);
284 final int customClientType =
object.optInt(KEY_CUSTOM_CLIENT_TYPE);
286 return new LoginInfoBuilder(account, token, authType, loginExt).withAppKey(appKey).withCustomClientType(customClientType).build();
290 public static final class LoginInfoBuilder {
291 private String account;
292 private String token;
294 private int authType;
295 private String loginExt;
297 private String appKey;
298 private int customClientType;
300 public LoginInfoBuilder(String account, String token,
int authType, String loginExt) {
301 this.account = account;
304 this.authType = authType;
305 this.loginExt = loginExt;
308 public static LoginInfoBuilder loginInfoDefault(String account, String token) {
309 return new LoginInfoBuilder(account, token, IAuthService.AuthType.DEFAULT,
"");
312 public static LoginInfoBuilder loginInfoDynamic(String account, String token) {
313 return new LoginInfoBuilder(account, token, IAuthService.AuthType.DYNAMIC,
"");
316 public static LoginInfoBuilder loginInfoThirdParty(String account, String token, String loginExt) {
317 return new LoginInfoBuilder(account, token, IAuthService.AuthType.THIRDPARTY, loginExt);
320 public static LoginInfoBuilder loginInfoThirdPartyDynamic(String account, String token) {
321 return new LoginInfoBuilder(account, token, IAuthService.AuthType.THIRDPARTY,
"");
324 public LoginInfoBuilder withAppKey(String appKey) {
325 this.appKey = appKey;
329 public LoginInfoBuilder withCustomClientType(
int customClientType) {
330 this.customClientType = customClientType;
334 public LoginInfoBuilder withAuthType(
int authType) {
335 this.authType = authType;
339 public LoginInfoBuilder withLoginExt(String loginExt) {
340 this.loginExt = loginExt;
347 loginInfo.authType = this.authType;
348 loginInfo.loginExt = this.loginExt;
350 loginInfo.appKey = this.appKey;
351 loginInfo.customClientType = this.customClientType;
static LoginInfo fromJson(JSONObject object)
int getAuthType()
SDK 登录 IM 的鉴权方式。
LoginInfo(String account, String token, String appKey)
构造用户登录信息。
int getCustomClientType()
自定义客户端类型,对应云信控制台中的自定义终端类型(需提前在控制台添加)。小于等于 0 视为没有自定义类...
boolean valid()
检查用户登录信息,验证该用户是否已存在,该用户信息是否有效。
String getAppKey()
当前应用的 AppKey。
void writeToParcel(Parcel dest, int flags)
String getAccount()
用户账号,对应云信 IM 账号 accid。
LoginInfo(String account, String token, String appKey, int customClientType)
构造用户登录信息。
LoginInfo(Parcel in)
构造用户登录信息。
LoginInfo(String account, String token)
构造用户登录信息。
String getLoginExt()
登录自定义扩展字段,用于第三方服务器鉴权。当 authType 为 2 时,必须传入该字段。
static final Creator< LoginInfo > CREATOR
String getToken()
登录 token。