Skip to content

Commit

Permalink
apply code style to all src files
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Apr 21, 2023
1 parent 113b945 commit 30c7a8f
Show file tree
Hide file tree
Showing 23 changed files with 1,620 additions and 1,622 deletions.
414 changes: 207 additions & 207 deletions src/main/java/io/github/coffeelibs/tinyoauth2client/AuthFlow.java

Large diffs are not rendered by default.

58 changes: 29 additions & 29 deletions src/main/java/io/github/coffeelibs/tinyoauth2client/PKCE.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,34 @@
*/
class PKCE {

public static final String METHOD = "S256";

private final String challenge;
private final String verifier;

public PKCE() {
// https://datatracker.ietf.org/doc/html/rfc7636#section-4
this.verifier = RandomUtil.randomToken(43);
this.challenge = Base64.getUrlEncoder().withoutPadding().encodeToString(sha256(verifier.getBytes(StandardCharsets.US_ASCII)));
}

public String getChallenge() {
return challenge;
}

public String getVerifier() {
return verifier;
}

private static byte[] sha256(byte[] input) {
try {
var digest = MessageDigest.getInstance("SHA-256");
return digest.digest(input);
} catch (NoSuchAlgorithmException e) {
// "Every implementation of the JDK 11 platform must support the specified algorithms [...]: SHA-256"
// see https://docs.oracle.com/en/java/javase/11/docs/specs/security/standard-names.html#security-algorithm-implementation-requirements
throw new AssertionError(e);
}
}
public static final String METHOD = "S256";

private final String challenge;
private final String verifier;

public PKCE() {
// https://datatracker.ietf.org/doc/html/rfc7636#section-4
this.verifier = RandomUtil.randomToken(43);
this.challenge = Base64.getUrlEncoder().withoutPadding().encodeToString(sha256(verifier.getBytes(StandardCharsets.US_ASCII)));
}

public String getChallenge() {
return challenge;
}

public String getVerifier() {
return verifier;
}

private static byte[] sha256(byte[] input) {
try {
var digest = MessageDigest.getInstance("SHA-256");
return digest.digest(input);
} catch (NoSuchAlgorithmException e) {
// "Every implementation of the JDK 11 platform must support the specified algorithms [...]: SHA-256"
// see https://docs.oracle.com/en/java/javase/11/docs/specs/security/standard-names.html#security-algorithm-implementation-requirements
throw new AssertionError(e);
}
}

}
35 changes: 18 additions & 17 deletions src/main/java/io/github/coffeelibs/tinyoauth2client/TinyOAuth2.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@
*/
public class TinyOAuth2 {

private TinyOAuth2() {
}
private TinyOAuth2() {
}

/**
* Begins building a new Tiny OAuth2 Client
* @param clientId Public <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-2.2">Client Identifier</a>
* @return A new {@link TinyOAuth2Client} Builder
*/
public static TinyOAuth2ClientWithoutTokenEndpoint client(String clientId) {
return tokenEndpoint -> new TinyOAuth2Client(clientId, tokenEndpoint);
}
/**
* Begins building a new Tiny OAuth2 Client
*
* @param clientId Public <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-2.2">Client Identifier</a>
* @return A new {@link TinyOAuth2Client} Builder
*/
public static TinyOAuth2ClientWithoutTokenEndpoint client(String clientId) {
return tokenEndpoint -> new TinyOAuth2Client(clientId, tokenEndpoint);
}

public interface TinyOAuth2ClientWithoutTokenEndpoint {
public interface TinyOAuth2ClientWithoutTokenEndpoint {

/**
* @param tokenEndpoint The URI of the <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-3.2">Token Endpoint</a>
* @return A new client
*/
TinyOAuth2Client withTokenEndpoint(URI tokenEndpoint);
}
/**
* @param tokenEndpoint The URI of the <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-3.2">Token Endpoint</a>
* @return A new client
*/
TinyOAuth2Client withTokenEndpoint(URI tokenEndpoint);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package io.github.coffeelibs.tinyoauth2client;

import io.github.coffeelibs.tinyoauth2client.util.URIUtil;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.BlockingExecutor;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.VisibleForTesting;
import org.jetbrains.annotations.*;

import java.io.IOException;
import java.net.URI;
Expand All @@ -25,83 +21,83 @@
@ApiStatus.Experimental
public class TinyOAuth2Client {

/**
* @see <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-2.2">Client Identifier</a>
*/
final String clientId;
/**
* @see <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-2.2">Client Identifier</a>
*/
final String clientId;

/**
* @see <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-3.2">Token Endpoint</a>
*/
final URI tokenEndpoint;
/**
* @see <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-3.2">Token Endpoint</a>
*/
final URI tokenEndpoint;

TinyOAuth2Client(String clientId, URI tokenEndpoint) {
this.clientId = Objects.requireNonNull(clientId);
this.tokenEndpoint = Objects.requireNonNull(tokenEndpoint);
}
TinyOAuth2Client(String clientId, URI tokenEndpoint) {
this.clientId = Objects.requireNonNull(clientId);
this.tokenEndpoint = Objects.requireNonNull(tokenEndpoint);
}

/**
* Initializes a new Authentication Code Flow with <a href="https://datatracker.ietf.org/doc/html/rfc7636">PKCE</a>
*
* @param authEndpoint The URI of the <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-3.1">Authorization Endpoint</a>
* @return A new Authentication Flow
*/
public AuthFlow authFlow(URI authEndpoint) {
return new AuthFlow(this, authEndpoint, new PKCE());
}
/**
* Initializes a new Authentication Code Flow with <a href="https://datatracker.ietf.org/doc/html/rfc7636">PKCE</a>
*
* @param authEndpoint The URI of the <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-3.1">Authorization Endpoint</a>
* @return A new Authentication Flow
*/
public AuthFlow authFlow(URI authEndpoint) {
return new AuthFlow(this, authEndpoint, new PKCE());
}

/**
* Refreshes an access token using the given {@code refreshToken}.
*
* @param executor The executor to run the async tasks
* @param refreshToken The refresh token
* @param scopes The desired access token <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-3.3">scopes</a>
* @return The future <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.4">Access Token Response</a>
* @see #refresh(String, String...)
*/
public CompletableFuture<HttpResponse<String>> refreshAsync(@BlockingExecutor Executor executor, String refreshToken, String... scopes) {
return HttpClient.newBuilder().executor(executor).build().sendAsync(buildRefreshTokenRequest(refreshToken, scopes), HttpResponse.BodyHandlers.ofString());
}
/**
* Refreshes an access token using the given {@code refreshToken}.
*
* @param executor The executor to run the async tasks
* @param refreshToken The refresh token
* @param scopes The desired access token <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-3.3">scopes</a>
* @return The future <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.4">Access Token Response</a>
* @see #refresh(String, String...)
*/
public CompletableFuture<HttpResponse<String>> refreshAsync(@BlockingExecutor Executor executor, String refreshToken, String... scopes) {
return HttpClient.newBuilder().executor(executor).build().sendAsync(buildRefreshTokenRequest(refreshToken, scopes), HttpResponse.BodyHandlers.ofString());
}

/**
* Refreshes an access token using the given {@code refreshToken}.
*
* @param refreshToken The refresh token
* @param scopes The desired access token <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-3.3">scopes</a>
* @return The <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.4">Access Token Response</a>
* @throws IOException In case of I/O errors when communicating with the token endpoint
* @throws InterruptedException When this thread is interrupted before a response is received
* @see <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-6">RFC 6749 Section 6: Refreshing an Access Token</a>
* @see #refreshAsync(Executor, String, String...)
*/
@Blocking
public HttpResponse<String> refresh(String refreshToken, String... scopes) throws IOException, InterruptedException {
return HttpClient.newHttpClient().send(buildRefreshTokenRequest(refreshToken, scopes), HttpResponse.BodyHandlers.ofString());
}
/**
* Refreshes an access token using the given {@code refreshToken}.
*
* @param refreshToken The refresh token
* @param scopes The desired access token <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-3.3">scopes</a>
* @return The <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.4">Access Token Response</a>
* @throws IOException In case of I/O errors when communicating with the token endpoint
* @throws InterruptedException When this thread is interrupted before a response is received
* @see <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-6">RFC 6749 Section 6: Refreshing an Access Token</a>
* @see #refreshAsync(Executor, String, String...)
*/
@Blocking
public HttpResponse<String> refresh(String refreshToken, String... scopes) throws IOException, InterruptedException {
return HttpClient.newHttpClient().send(buildRefreshTokenRequest(refreshToken, scopes), HttpResponse.BodyHandlers.ofString());
}

@VisibleForTesting
HttpRequest buildRefreshTokenRequest(String refreshToken, String... scopes) {
return buildTokenRequest(Map.of(//
"grant_type", "refresh_token", //
"refresh_token", refreshToken, //
"client_id", clientId, //
"scope", String.join(" ", scopes)
));
}
@VisibleForTesting
HttpRequest buildRefreshTokenRequest(String refreshToken, String... scopes) {
return buildTokenRequest(Map.of(//
"grant_type", "refresh_token", //
"refresh_token", refreshToken, //
"client_id", clientId, //
"scope", String.join(" ", scopes)
));
}

/**
* Creates a new HTTP request targeting the {@link #tokenEndpoint}.
*
* @param parameters Parameters to send in an {@code application/x-www-form-urlencoded} request body
* @return A new http request
*/
@Contract("_ -> new")
HttpRequest buildTokenRequest(Map<String, String> parameters) {
var urlencodedParams = URIUtil.buildQueryString(parameters);
return HttpRequest.newBuilder(tokenEndpoint) //
.header("Content-Type", "application/x-www-form-urlencoded") //
.POST(HttpRequest.BodyPublishers.ofString(urlencodedParams)) //
.build();
}
/**
* Creates a new HTTP request targeting the {@link #tokenEndpoint}.
*
* @param parameters Parameters to send in an {@code application/x-www-form-urlencoded} request body
* @return A new http request
*/
@Contract("_ -> new")
HttpRequest buildTokenRequest(Map<String, String> parameters) {
var urlencodedParams = URIUtil.buildQueryString(parameters);
return HttpRequest.newBuilder(tokenEndpoint) //
.header("Content-Type", "application/x-www-form-urlencoded") //
.POST(HttpRequest.BodyPublishers.ofString(urlencodedParams)) //
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import io.github.coffeelibs.tinyoauth2client.http.response.Response;

public class InvalidRequestException extends Exception {
public final Response suggestedResponse;
public final Response suggestedResponse;

public InvalidRequestException(Response suggestedResponse) {
this.suggestedResponse = suggestedResponse;
}
public InvalidRequestException(Response suggestedResponse) {
this.suggestedResponse = suggestedResponse;
}

}

0 comments on commit 30c7a8f

Please sign in to comment.