# Authing-Java / Kotlin

The Authing Java SDK consists of two parts: ManagementClient and AuthenticationClient. All operations performed in ManagementClient are performed as an administrator, including modules such as managing users, managing roles, managing authority policies, and managing user pool configuration. All operations in AuthenticationClient are performed as the current terminal user, including methods such as login, registration, modification of user information, and logout.

You should set the initialized ManagementClient instance to a global variable (initialize only once), and AuthenticationClient should be initialized one for every request.

# Installation

# gradle project

Add in dependencies in build.gradle:

implementation "cn.authing:java-core:<LATEST_VERSION>"

You can get the latest version at https://search.maven.org/artifact/cn.authing/java-core (opens new window) .

# maven project

Add to dependencies in pom.xml:

If you are building a spring app, you must specify OkHttp version at pom.xml.

<dependency>
    <groupId>cn.authing</groupId>
    <artifactId>java-core</artifactId>
    <version><LATEST_VERSION></version>
</dependency>
<properties>
    <okhttp3.version>4.8.0</okhttp3.version>
</properties>

# Using the management module

Initializing ManagementClient requires userPoolId (user pool ID) and secret (user pool key):

You can click here Learn how to get UserPoolId and Secret (opens new window).

import cn.authing.core.mgmt.ManagementClient;

public class ManagementClientTest {
    public static void main(String[] args){
      ManagementClient managementClient = new ManagementClient("AUTHING_USERPOOL_ID", "AUTHING_USERPOOL_SECRET");
      
      // Get admin rights
      managementClient.requestToken().execute();
    }
}

Now the managementClient instance is ready to use. For example, you can get the list of users in the user pool:

import cn.authing.core.mgmt.ManagementClient;

public class ManagementClientTest {
    public static void main(String[] args){
        ManagementClient managementClient = new ManagementClient("AUTHING_USERPOOL_ID", "AUTHING_USERPOOL_SECRET");
        // Get admin rights
        managementClient.requestToken().execute();

        PaginatedUsers users = managementClient.users().list().execute();
    }
}

# Use authentication module

Initializing ManagementClient requires userPoolId (user pool ID) and appId (application ID):

Here you can learn how to get UserPoolId (opens new window), view your own application list in Applications of the console.

import cn.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("AUTHING_USERPOOL_ID");
        authenticationClient.setAppId("AUTHING_APP_ID");
    }
}

Next, you can perform operations such as registration and login:

import cn.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("AUTHING_USERPOOL_ID");
        authenticationClient.setAppId("AUTHING_APP_ID");

        String email = "test@example.com";
        String password = "123456";
        User user = authenticationClient.registerByEmail(new RegisterByEmailInput(email, password)).execute();
    }
}

After logging in, methods such as update_profile that require users to log in are available:

import cn.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("AUTHING_USERPOOL_ID");
        authenticationClient.setAppId("AUTHING_APP_ID");

        String email = "test@example.com";
        String password = "123456";
        authenticationClient.loginByEmail(new LoginByEmailInput(email, password)).execute();

        User user = authenticationClient.updateProfile(new UpdateUserInput().withNickname("nickname")).execute();
    }
}

You can also set the AccessToken parameter after initialization, without calling the LoginByXXX method every time:

import cn.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("AUTHING_USERPOOL_ID");
        authenticationClient.setAppId("AUTHING_APP_ID");
        authenticationClient.setAccessToken("ACCESS_TOKEN");
    }
}

Execute the UpdateProfile method again and find that it is also successful:

import cn.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("AUTHING_USERPOOL_ID");
        authenticationClient.setAccessToken("ACCESS_TOKEN");
        User user = authenticationClient.updateProfile(new UpdateUserInput().withNickname("nickname")).execute();
    }
}

# Error handling

import cn.authing.core.auth.AuthenticationClient;
import cn.authing.core.graphql.GraphQLException;
import java.io.IOException;


public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("AUTHING_USERPOOL_ID");
        authenticationClient.setAccessToken("ACCESS_TOKEN");

        try {
            User user = authenticationClient.updateProfile(new UpdateUserInput().withNickname("nickname")).execute();
        } catch (GraphQLException | IOException e) {
            e.printStackTrace();
        }
    }
}

# Privatization deployment

If you are in a privatization deployment scenario and need to customize the access domain name and public key, you can perform additional configuration when initializing ManagementClient and AuthenticationClient:

# Using the management module

Initializing ManagementClient requires userPoolId (user pool ID) and secret (user pool key):

import cn.authing.core.mgmt.ManagementClient;

public class ManagementClientTest {
    public static void main(String[] args){
      ManagementClient managementClient = new ManagementClient("AUTHING_USERPOOL_ID", "AUTHING_USERPOOL_SECRET");
      // Configure custom domain name
      managementClient.setHost("custom host");
      // Configure custom public key
      managementClient.setPublicKey("public key");
      
      // Get admin rights
      managementClient.requestToken().execute();
    }
}

# Use authentication module

Initializing ManagementClient requires userPoolId (user pool ID):

import cn.authing.core.auth.AuthenticationClient;

public class AuthenticationClientTest {
    public static void main(String[] args){
        AuthenticationClient authenticationClient = new AuthenticationClient("AUTHING_USERPOOL_ID");
        authenticationClient.setAppId("AUTHING_APP_ID");
        // Configure custom domain name
        authenticationClient.setHost("custom host");
        // Configure custom public key
        authenticationClient.setPublicKey("public key");
    }
}

# Get help

Join us on Gitter: #authing-chat (opens new window)

# Interface Index

Available Authentication methods

  • Get the user information of the current user: getCurrentUser
  • Register with email: registerByEmail
  • Register with username: registerByUsername
  • Register with mobile phone number verification code: registerByPhoneCode
  • Login with email: loginByEmail
  • Login with username: loginByUsername
  • Use mobile phone number verification code to log in to loginByPhoneCode
  • Use mobile phone number password to log in: loginByPhonePassword
  • Send email: sendEmail
  • Send SMS verification code: sendSmsCode
  • Check the valid status of the token: checkLoginStatus
  • Use phone number verification code to reset password: resetPasswordByPhoneCode
  • Use email verification code to reset password: resetPasswordByEmailCode
  • Update user profile: updateProfile
  • Update password: updatePassword
  • Update phone number: updatePhone
  • Update email: updateEmail
  • Refresh token: refreshToken
  • Bind mobile phone number: bindPhone
  • Unbind phone number: unbindPhone

For details, see:

Core Authentication Module

The management module contains the following sub-modules:

Manage users Manage Roles Manage Strategy Manage Authority & Access Control Manage custom field metadata Management group Management organization Manage user pool configuration Manage registration whitelist Management application