NIMSDK-AOS  9.16.3
LoginInfo.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.auth;
2 
3 import android.os.Parcel;
4 import android.os.Parcelable;
5 import android.text.TextUtils;
6 
7 import androidx.annotation.NonNull;
8 
9 import com.netease.nimlib.SDKCache;
10 import com.netease.nimlib.biz.constant.IAuthService;
11 import com.netease.nimlib.log.NimLog;
12 import com.netease.nimlib.util.StringUtil;
13 
14 import org.json.JSONException;
15 import org.json.JSONObject;
16 
17 import java.io.Serializable;
18 import java.util.Objects;
19 
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";
30 
31 
32  private final String account;
33 
34  private final String token;
35 
36  private int authType;
37 
38  private String loginExt;
39 
40  private String appKey;
41 
42  private int customClientType;
43 
62  public LoginInfo(String account, String token) {
63  this.account = account == null ? null : account.toLowerCase();
64  this.token = token;
65  }
66 
89  public LoginInfo(String account, String token, String appKey) {
90  this(account, token);
91  this.appKey = appKey;
92  }
93 
94 
95 
122  public LoginInfo(String account, String token, String appKey, int customClientType) {
123  this(account, token, appKey);
124  this.customClientType = customClientType;
125  }
126 
132  protected LoginInfo(Parcel in) {
133  account = in.readString();
134  token = in.readString();
135 
136  authType = in.readInt();
137  loginExt = in.readString();
138 
139  appKey = in.readString();
140  customClientType = in.readInt();
141  }
142 
143  public static final Creator<LoginInfo> CREATOR = new Creator<LoginInfo>() {
144  @Override
145  public LoginInfo createFromParcel(Parcel in) {
146  return new LoginInfo(in);
147  }
148 
149  @Override
150  public LoginInfo[] newArray(int size) {
151  return new LoginInfo[size];
152  }
153  };
154 
158  public String getAccount() {
159  return account;
160  }
161 
165  public String getToken() {
166  return token;
167  }
168 
175  public int getAuthType() {
176  return authType;
177  }
178 
182  public String getLoginExt() {
183  return loginExt;
184  }
185 
189  public String getAppKey() {
190  return appKey;
191  }
192 
196  public int getCustomClientType() {
197  return customClientType;
198  }
199 
205  public boolean valid() {
206  if (authType == IAuthService.AuthType.DEFAULT) {
207  return !TextUtils.isEmpty(account) && !TextUtils.isEmpty(token);
208  } else {
209  return !TextUtils.isEmpty(account);
210  }
211  }
212 
213  @Override
214  public boolean equals(Object o) {
215  if (this == o) return true;
216  if (o == null || getClass() != o.getClass()) return false;
217  LoginInfo loginInfo = (LoginInfo) o;
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);
224  }
225 
226 
227  @NonNull
228  @Override
229  public String toString() {
230  return "LoginInfo{" +
231  "account='" + account + '\'' +
232  ", authType=" + authType +
233  ", customClientType=" + customClientType +
234  '}';
235  }
236 
237  @Override
238  public int hashCode() {
239  return Objects.hash(account, token, authType, loginExt, appKey, customClientType);
240  }
241 
242  @Override
243  public int describeContents() {
244  return 0;
245  }
246 
247  @Override
248  public void writeToParcel(Parcel dest, int flags) {
249  dest.writeString(account);
250  dest.writeString(token);
251 
252  dest.writeInt(authType);
253  dest.writeString(loginExt);
254 
255  dest.writeString(appKey);
256  dest.writeInt(customClientType);
257  }
258 
259  public JSONObject toJson() {
260  JSONObject object = new JSONObject();
261  try {
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) {
269  e.printStackTrace();
270  }
271  return object;
272  }
273 
274  public static LoginInfo fromJson(JSONObject object) {
275  if (object == null) {
276  return null;
277  }
278 
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);
285 
286  return new LoginInfoBuilder(account, token, authType, loginExt).withAppKey(appKey).withCustomClientType(customClientType).build();
287  }
288 
289 
290  public static final class LoginInfoBuilder {
291  private String account;
292  private String token;
293 
294  private int authType;
295  private String loginExt;
296 
297  private String appKey;
298  private int customClientType;
299 
300  public LoginInfoBuilder(String account, String token, int authType, String loginExt) {
301  this.account = account;
302  this.token = token;
303 
304  this.authType = authType;
305  this.loginExt = loginExt;
306  }
307 
308  public static LoginInfoBuilder loginInfoDefault(String account, String token) {
309  return new LoginInfoBuilder(account, token, IAuthService.AuthType.DEFAULT, "");
310  }
311 
312  public static LoginInfoBuilder loginInfoDynamic(String account, String token) {
313  return new LoginInfoBuilder(account, token, IAuthService.AuthType.DYNAMIC, "");
314  }
315 
316  public static LoginInfoBuilder loginInfoThirdParty(String account, String token, String loginExt) {
317  return new LoginInfoBuilder(account, token, IAuthService.AuthType.THIRDPARTY, loginExt);
318  }
319 
320  public static LoginInfoBuilder loginInfoThirdPartyDynamic(String account, String token) {
321  return new LoginInfoBuilder(account, token, IAuthService.AuthType.THIRDPARTY, "");
322  }
323 
324  public LoginInfoBuilder withAppKey(String appKey) {
325  this.appKey = appKey;
326  return this;
327  }
328 
329  public LoginInfoBuilder withCustomClientType(int customClientType) {
330  this.customClientType = customClientType;
331  return this;
332  }
333 
334  public LoginInfoBuilder withAuthType(int authType) {
335  this.authType = authType;
336  return this;
337  }
338 
339  public LoginInfoBuilder withLoginExt(String loginExt) {
340  this.loginExt = loginExt;
341  return this;
342  }
343 
344  public LoginInfo build() {
345  LoginInfo loginInfo = new LoginInfo(account, token);
346 
347  loginInfo.authType = this.authType;
348  loginInfo.loginExt = this.loginExt;
349 
350  loginInfo.appKey = this.appKey;
351  loginInfo.customClientType = this.customClientType;
352  return loginInfo;
353  }
354  }
355 }
static LoginInfo fromJson(JSONObject object)
Definition: LoginInfo.java:274
int getAuthType()
SDK 登录 IM 的鉴权方式。
Definition: LoginInfo.java:175
LoginInfo(String account, String token, String appKey)
构造用户登录信息。
Definition: LoginInfo.java:89
int getCustomClientType()
自定义客户端类型,对应云信控制台中的自定义终端类型(需提前在控制台添加)。小于等于 0 视为没有自定义类...
Definition: LoginInfo.java:196
boolean valid()
检查用户登录信息,验证该用户是否已存在,该用户信息是否有效。
Definition: LoginInfo.java:205
String getAppKey()
当前应用的 AppKey。
Definition: LoginInfo.java:189
void writeToParcel(Parcel dest, int flags)
Definition: LoginInfo.java:248
String getAccount()
用户账号,对应云信 IM 账号 accid。
Definition: LoginInfo.java:158
LoginInfo(String account, String token, String appKey, int customClientType)
构造用户登录信息。
Definition: LoginInfo.java:122
用户登录认证信息。
Definition: LoginInfo.java:23
LoginInfo(Parcel in)
构造用户登录信息。
Definition: LoginInfo.java:132
LoginInfo(String account, String token)
构造用户登录信息。
Definition: LoginInfo.java:62
String getLoginExt()
登录自定义扩展字段,用于第三方服务器鉴权。当 authType 为 2 时,必须传入该字段。
Definition: LoginInfo.java:182
static final Creator< LoginInfo > CREATOR
Definition: LoginInfo.java:143
String getToken()
登录 token。
Definition: LoginInfo.java:165