This is my personal OAuth2 library for use in locally installed java applications. It is designed to be as simple as possible. The only dependency is on FasterXML/jackson for JSON parsing.
I wanted to enable an application I developed to use OAuth2 and I couldn't find a slim and easy to use library. That's why I created my own.
- Include the dependency in your pom (or the equivalent for your build tool):
<dependency>
<groupId>net.nikomitk</groupId>
<artifactId>ssoauth2-client</artifactId>
<version>0.1</version>
</dependency>- Create an instance of a flow and initialize it:
import net.nikomitk.ssoauth2.OAuth2Flow;
import net.nikomitk.ssoauth2.flows.OAuth2BrowserFlow;
import net.nikomitk.ssoauth2.security.PKCE;
OAuth2Flow flow = new OAuth2BrowserFlow("https://sso.example.com/.well-known/openid-configuration", "client-id", "openid profile email", PKCE.S256);
flow.startFlow(); // Initialization- Execute the needed steps as often as you need (you can store the flow instance in order to periodically refresh your token:
flow.requestAccessToken(); // Request the access token from the authorization server
flow.refreshAccessToken(); // Refresh the access token in a way suitable for your chosen flow
flow.getAccessToken(); // Getter for the access token stored in the flowFor simpler use, you can also chain these methods:
flow.requestAccessToken().refreshAccessToken().getAccessToken(); // Getter for the access token stored in the flowCurrently, the following flows are available:
- OAuth2AuthCodeFlow (Section 4.1 of RFC6749, optionally with PKCE)
- Starts an http server on a random port (or the one passed to
startFlow(int port)) with redirection url of "http://127.0.0.1:<port>/oauth2/redirect". The login URL the user has to open is returned bygetAuthUri().
- Starts an http server on a random port (or the one passed to
- OAuth2BrowserFlow
- Same as OAuth2AuthCodeFlow, but opens the login URL in the users default browser automatically.
- OAuth2UserPasswordFlow (Section 4.3 of RFC6749)
- Gets an access token directly using a username and password. Should not be used in production.
refreshAccessToken()requests a new token with the same credentials.
- Gets an access token directly using a username and password. Should not be used in production.
I plan to add more in the future, but I currently don't have enough time. Contributions are welcome.