-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
824 additions
and
461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
242 changes: 105 additions & 137 deletions
242
src/main/java/io/github/coffeelibs/tinyoauth2client/AuthFlow.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/io/github/coffeelibs/tinyoauth2client/TinyOAuth2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.github.coffeelibs.tinyoauth2client; | ||
|
||
import java.net.URI; | ||
|
||
/** | ||
* Fluent builder for a {@link TinyOAuth2Client} | ||
*/ | ||
public class 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); | ||
} | ||
|
||
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); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/io/github/coffeelibs/tinyoauth2client/TinyOAuth2Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.github.coffeelibs.tinyoauth2client; | ||
|
||
import io.github.coffeelibs.tinyoauth2client.util.URIUtil; | ||
import org.jetbrains.annotations.Blocking; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* An OAuth2 <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-2.1">public client</a> capable of making requests to a token endpoint. | ||
* | ||
* @see TinyOAuth2#client(String) | ||
*/ | ||
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-3.2">Token Endpoint</a> | ||
*/ | ||
final URI 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()); | ||
} | ||
|
||
/** | ||
* 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 raw <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> | ||
*/ | ||
@Blocking | ||
public String refresh(String refreshToken, String... scopes) throws IOException, InterruptedException { | ||
var requestBody = URIUtil.buildQueryString(Map.of(// | ||
"grant_type", "refresh_token", // | ||
"refresh_token", refreshToken, // | ||
"client_id", clientId, // | ||
"scope", String.join(" ", scopes) | ||
)); | ||
var request = HttpRequest.newBuilder(tokenEndpoint) // | ||
.header("Content-Type", "application/x-www-form-urlencoded") // | ||
.POST(HttpRequest.BodyPublishers.ofString(requestBody)) // | ||
.build(); | ||
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); | ||
if (response.statusCode() == 200) { | ||
return response.body(); | ||
} else { | ||
throw new IOException("Unexpected HTTP response code " + response.statusCode()); | ||
} | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/io/github/coffeelibs/tinyoauth2client/http/InvalidRequestException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
.../tinyoauth2client/http/EmptyResponse.java → ...h2client/http/response/EmptyResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
...s/tinyoauth2client/http/HtmlResponse.java → ...th2client/http/response/HtmlResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
...nyoauth2client/http/RedirectResponse.java → ...lient/http/response/RedirectResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.