nrktkt/OAuth2


Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.kag0:oauth2:1.1.4'
	}
	dependencies {
		implementation("com.github.kag0:oauth2:1.1.4")
	}
	<dependency>
	    <groupId>com.github.kag0</groupId>
	    <artifactId>oauth2</artifactId>
	    <version>1.1.4</version>
	</dependency>

                            
    libraryDependencies += "com.github.kag0" % "oauth2" % "1.1.4"
        
        

                            
    :dependencies [[com.github.kag0/oauth2 "1.1.4"]]
        
        

Readme


OAuth2

oauth2 logo

Codacy Badge
Build Status

codecov

A set of extensible immutable models for communicating with OAuth 2. This library provides the ability to act in the resource server, client, or authorization server roles defined by OAuth 2.

Example of general usage to act in authorization server role

TokenRequest request = TokenRequest.parseEncoded(requestBody).get();
if(request instanceof PasswordTokenRequest){
	PasswordTokenRequest passwordRequest = (PasswordTokenRequest) request;
	// confirm that passwordRequest.username() passwordRequest.password() are valid credentials
	if(validCredentials){
		TokenResponse response = ImmutableTokenResponse.builder()
			.accessToken("new token")
			.exipresIn(600)
			.scope(passwordRequest.scope())
			.tokenType(Bearer)
			.build();
		//return response
	}else{
		ErrorResponse response = ImmutableErrorResponse.of(invalid_grant);
		//return response
	}
}

Example of general usage to obtain authorization to access google+ user info

AuthorizationRequest authzRequest = ImmutableAuthorizationRequest.builder()
	.redirectUri(myRedirectEndpoint)
	.clientId(myClientId)
	.scope(HashSet.of(
		"https://www.googleapis.com/auth/plus.login"
		,"https://www.googleapis.com/auth/plus.me"
		,"https://www.googleapis.com/auth/userinfo.email"
		,"https://www.googleapis.com/auth/userinfo.profile"
	)).build();

URI redirect = URI.create("https://accounts.google.com/o/oauth2/v2/auth?" + authzRequest.toFormEncoded());

Redirect caller to redirect

Either<ErrorResponse, AuthorizationResponse> errorOrAuthzResponse = AuthorizationResponse.parseEncoded(request.getQuery());
if(errorOrAuthzResponse.isLeft())
	//handle error
else{
	TokenRequest tokenRequest = ImmutableSecretClientTokenRequest.of(
		ImmutableCodeTokenRequest.builder()
			.clientId(myClientId)
			.code(errorOrAuthzResponse.get().code())
			.redirectUri(myRedirectEndpoint)
			.build()
		,myClientSecret
	);
}
// POST to www.googleapis.com/oauth2/v4/token with tokenRequest.toFormEncoded() in body
TokenResponse tokenResponse = TokenResponse.parse(responseBody).get;

Make requests to api with tokenResponse.tokenType().name() +' '+ tokenResponse.accessToken() in Authorization header