¶ OIDC API
¶ build login URL
Use this API to generate login url, then pass this url to Webview
public static String buildAuthorizeUrl(Config config, AuthRequest authRequest)
Parameter
config
application configuration, obtained by Authing.getPublicConfigauthRequest
auth request object
example
AuthRequest authRequest = new AuthRequest();
Authing.getPublicConfig(config -> {
String url = OIDCClient.buildAuthorizeUrl(config, authRequest);
myWebView.loadUrl(url);
});
set scope
use this API to set OIDC scope. Default scope is: openid profile email phone username address offline_access role extended_fields
authRequest.setScope(String scope)
set redirect url
SDK will get the default redirect url from console. Use this API if you want to use a specific redirect url.
authRequest.setRedirectURL(String redirectURL)
¶ get token by auth code
This API returns token(s) by auth code. Note that in order to return refresh token make sure the scope includes offline_access, which is included by default.
public static void authByCode(String code, AuthRequest authRequest, @NotNull AuthCallback<UserInfo> callback)
Parameter
code
OIDC auth codeauthRequest
auth request object
example
myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (url.startsWith(authRequest.getRedirectURL())) {
try {
String authCode = Util.getAuthCode(url);
if (authCode != null) {
OIDCClient.authByCode(authCode, authRequest, (code, message, userInfo) -> {
// got user info
});
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
return false;
}
});
¶ Get user info
Get detailed user info by access token. The returned UserInfo object is the same as the UserInfo object in parameter.
public static void getUserInfoByAccessToken(UserInfo userInfo, @NotNull AuthCallback<UserInfo> callback)
Parameter
userInfo
includes access tokencallback
returns detailed user info if succeeds
example
OIDCClient.getUserInfoByAccessToken(userInfo, (code, message, data)->{
if (code == 200) {
// data is the same object as the first parameter
}
});
¶ Obtain new access token and id token by refresh token
the valid duration of an access token is usually short. After it expires, instead of pop up login dialog, which is not very user friendly, we should use refresh token to get new access token. Only show login page when refresh token is expired.
public static void getNewAccessTokenByRefreshToken(String refreshToken, @NotNull AuthCallback<UserInfo> callback)
Parameter
refreshToken
refresh tokencallback
includes new access token and id token
example
OIDCClient.getNewAccessTokenByRefreshToken(rt, (code, message, data)->{
if (code == 200) {
Log.d(TAG, "new at:" + data.getAccessToken());
Log.d(TAG, "new id token:" + data.getIdToken());
Log.d(TAG, "new rt:" + data.getRefreshToken());
}
});
Note: refresh token will also be refreshed
¶ Get Access Token、ID Token 和 Refresh Token
¶ Use email registration
Use the email registration, the mailbox is not case sensitive and the only userpool is unique. This interface does not require the user to verify the mailbox, after the user registration, the emailVerified field will be false.
public void registerByEmail(String email, String password, String context, @NotNull AuthCallback<UserInfo> callback)
Parameter
email
email addresspassword
passwordcontext
Request context, set herecontext
you can get pipeline context (opens new window). This parameter can be passed tonull
if not required.
example
JSONObject context = new JSONObject();
context.put("userId", "userId");
new OIDCClient().registerByEmail("test@example.com", "xxxxxx", context.toString(), (code, message, userInfo)->{
if (code == 200) {
// userInfo
}
});
Error Code
2003
Illegal email address2026
Registered mailbox
¶ Use email code registration
Use the email registration, the mailbox is not case sensitive and the only userpool is unique. This interface does not require the user to verify the mailbox, after the user registration, the emailVerified field will be false. You need to use it first sendEmail sends a email verification code.(scene is VERIFY_CODE
).
public void registerByEmailCode(String email, String vCode, String context, @NotNull AuthCallback<UserInfo> callback)
Parameter
email
email addressvCode
codecontext
Request context, set herecontext
you can get pipeline context (opens new window). This parameter can be passed tonull
if not required.
example
JSONObject context = new JSONObject();
context.put("userId", "userId");
new OIDCClient().registerByEmailCode("test@example.com", "1234", context.toString(), (code, message, userInfo)->{
if (code == 200) {
// userInfo
}
});
Error Code
2003
Illegal email address2026
Registered mailbox
¶ Use mobile phone number registration
Use your mobile phone number to register, you can set the initial password of the account at the same time. You can pass sendSmsCode method sends SMS verification code.
public void registerByPhoneCode(String phoneCountryCode, String phone, String code, String password, String context, @NotNull AuthCallback<UserInfo> callback)
Parameter
phoneCountryCode
Telephone country code, If null, the default value is+86
.phone
The phone numbercode
SMS verification codepassword
initial password, it can benull
context
Request context, set herecontext
you can get pipeline context (opens new window). This parameter can be passed tonull
if not required.
example
JSONObject context = new JSONObject();
context.put("userId", "userId");
new OIDCClient().registerByPhoneCode("+86", "188xxxx8888", "1234", "strong", false, context.toString(), (code, message, userInfo)->{
if (code == 200) {
// userInfo
}
});
Error Code
2001
SMS verification code error2026
Cell phone number registered
¶ Custom field registration
You can directly log in to an account registered with a user-defined field using the account password.
public static void registerByExtendField(String fieldName, String account, String password, String context, @NotNull AuthCallback<UserInfo> callback)
Parameter
fieldName
custom field name
account
accountpassword
initial password, it can benull
context
Request context, set herecontext
you can get pipeline context (opens new window). This parameter can be passed tonull
if not required.
Example
JSONObject context = new JSONObject();
context.put("userId", "userId");
new OIDCClient().registerByExtendField("extendId", "188xxxx8888", "xxxxxx", context.toString(), (code, message, userInfo)->{
if (code == 200) {
// userInfo
}
});
Error Code
2026
The user name already exists
¶ Use the username to login
public void loginByAccount(String account, String password, boolean autoRegister, String context, @NotNull AuthCallback<UserInfo> callback)
Parameter
account
The phone number / email address / usernamepassword
passwordautoRegister
Whether it is automatically registered. If the user does not exist, an account is automatically created according to the login book.context
Request context, set herecontext
you can get pipeline context (opens new window). This parameter can be passed tonull
if not required.
Example
JSONObject context = new JSONObject();
context.put("userId", "userId");
new OIDCClient().loginByAccount("account", "xxxxxx", false, context.toString(), (code, message, userInfo)->{
if (code == 200) {
// userInfo
}
});
Error Code
2333
The account or password is incorrect
¶ Use email code to login
Use the email verification code to log in. You need to use it first sendEmail sends a email verification code.(scene isVERIFY_CODE
)。
public void loginByEmailCode(String email, String vCode, boolean autoRegister, String context, @NotNull AuthCallback<UserInfo> callback)
Parameter
email
email addressvCode
codeautoRegister
Whether it is automatically registered. If the user does not exist, an account is automatically created according to the login book.context
Request context, set herecontext
you can get pipeline context (opens new window). This parameter can be passed tonull
if not required.
Example
JSONObject context = new JSONObject();
context.put("userId", "userId");
new OIDCClient().loginByEmailCode("test@example.com", "1234", false, context.toString(), (code, message, userInfo)->{
if (code == 200) {
// userInfo
}
});
Error Code
2001
email verification code error
¶ Use the mobile phone number verification code to login
Use the mobile phone number verification code to log in. You need to use it first sendSmsCode sends a SMS verification code.
public void loginByPhoneCode(String phoneCountryCode, String phone, String code, boolean autoRegister, String context, @NotNull AuthCallback<UserInfo> callback)
Parameter
phoneCountryCode
Telephone country code, If null, the default value is +86phone
The phone numbercode
SMS verification codeautoRegister
Whether it is automatically registered. If the user does not exist, an account is automatically created according to the login book.context
Request context, set herecontext
you can get pipeline context (opens new window). This parameter can be passed tonull
if not required.
Example
JSONObject context = new JSONObject();
context.put("userId", "userId");
new OIDCClient().loginByPhoneCode("+86", "188xxxx8888", "1234", false, context.toString(), (code, message, userInfo)->{
if (code == 200) {
// userInfo
}
});
Error Code
2001
SMS verification code error