Java SDK

Official OAuth42 SDK for Java. Works with Spring Boot, Jakarta EE, and any Java application.

Installation

Maven

<dependency>
    <groupId>com.oauth42</groupId>
    <artifactId>oauth42-client</artifactId>
    <version>1.0.0</version>
</dependency>

Gradle

implementation 'com.oauth42:oauth42-client:1.0.0'

Quick Start

import com.oauth42.client.OAuth42Client;
import com.oauth42.client.OAuth42Config;

OAuth42Client client = new OAuth42Client(
    OAuth42Config.builder()
        .clientId(System.getenv("OAUTH42_CLIENT_ID"))
        .clientSecret(System.getenv("OAUTH42_CLIENT_SECRET"))
        .redirectUri("https://yourapp.com/callback")
        .build()
);

// Start authorization
AuthorizationData authData = client.authorize(
    new AuthorizeParams.Builder()
        .scope("openid", "profile", "email")
        .build()
);

// Exchange code for tokens
TokenResponse tokens = client.exchangeCode(
    new ExchangeParams.Builder()
        .code(code)
        .codeVerifier(codeVerifier)
        .build()
);

Spring Boot Integration

@RestController
public class AuthController {

    @Autowired
    private OAuth42Client oauth42Client;

    @GetMapping("/login")
    public RedirectView login(HttpSession session) {
        AuthorizationData authData = oauth42Client.authorize(
            new AuthorizeParams.Builder()
                .scope("openid", "profile", "email")
                .build()
        );

        session.setAttribute("code_verifier", authData.getCodeVerifier());
        session.setAttribute("state", authData.getState());

        return new RedirectView(authData.getUrl());
    }

    @GetMapping("/callback")
    public RedirectView callback(
        @RequestParam String code,
        @RequestParam String state,
        HttpSession session
    ) {
        String storedState = (String) session.getAttribute("state");
        if (!state.equals(storedState)) {
            throw new IllegalStateException("Invalid state");
        }

        TokenResponse tokens = oauth42Client.exchangeCode(
            new ExchangeParams.Builder()
                .code(code)
                .codeVerifier((String) session.getAttribute("code_verifier"))
                .build()
        );

        session.setAttribute("access_token", tokens.getAccessToken());
        return new RedirectView("/dashboard");
    }
}

Best Practices

Use Dependency Injection

Configure OAuth42Client as a Spring bean for reuse.

Handle Exceptions

Catch OAuth42Exception for OAuth-specific errors.

Next Steps