From 5f7d3e266a69f7f3ebd0a37bf0eb05bd5e1bb7fa Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Mon, 25 Apr 2022 16:30:36 +0200 Subject: [PATCH 1/9] typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8a789eb..4253ba7 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ support for [PKCE](https://datatracker.ietf.org/doc/html/rfc8252#section-8.1) an ## Usage ```java -// this library will just to the Authorization Flow: +// this library will just perform the Authorization Flow: String tokenResponse = AuthFlow.asClient("oauth-client-id") .authorize(URI.create("https://login.example.com/oauth2/authorize"), uri -> System.out.println("Please login on " + uri)) .getAccessToken(URI.create("https://login.example.com/oauth2/token")); From 5d8e134d725e370c2b857df683bfb456631c5f4c Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Thu, 28 Apr 2022 09:47:45 +0200 Subject: [PATCH 2/9] typo [ci skip] --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4253ba7..07d815e 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,6 @@ The `authorize(...)` method optionally allows you to specify: ## Why this library? * Often you just need to authorize your client and nothing more. Most OAuth2 libraries try to do a lot more -* Nano-tiny-minuscule attack surface, since this doesn't contain any JOSE/JWT signature code, nor a fully-fleged web server +* Nano-tiny-minuscule attack surface, since this doesn't contain any JOSE/JWT signature code, nor a fully-fledged web server * Focus is strictly on the authorization flow. Use any library for dealing with the tokens, you like. * Modular jar, exposing only one single public API. No need to read docs, you can't do anything wrong. \ No newline at end of file From ecbdddbcea37a09bd03d74bcb55dbe0591323d47 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Thu, 28 Apr 2022 09:50:34 +0200 Subject: [PATCH 3/9] implemented HttpHtmlResponseTest --- .../http/HttpHtmlResponseTest.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpHtmlResponseTest.java b/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpHtmlResponseTest.java index bdf7902..d50e12f 100644 --- a/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpHtmlResponseTest.java +++ b/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpHtmlResponseTest.java @@ -1,7 +1,26 @@ package io.github.coffeelibs.tinyoauth2client.http; -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.CharArrayWriter; +import java.io.IOException; public class HttpHtmlResponseTest { + @Test + public void testWrite() throws IOException { + var body = "Hello World"; + var response = new HttpHtmlResponse(HttpResponse.Status.OK, body); + var writer = new CharArrayWriter(); + + response.write(writer); + + var str = writer.toString(); + Assertions.assertTrue(str.startsWith("HTTP/1.1 200 OK")); + Assertions.assertTrue(str.contains("Content-Type: text/html; charset=UTF-8")); + Assertions.assertTrue(str.contains("Content-Length: " + body.length())); + Assertions.assertTrue(str.endsWith("Hello World\n")); + } + } \ No newline at end of file From 59346d9bc885645930fd6d694b05df5e745cf215 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Thu, 28 Apr 2022 09:52:46 +0200 Subject: [PATCH 4/9] renamed HttpResponse due to ambiguity --- ...pEmptyResponse.java => EmptyResponse.java} | 4 +-- ...ttpHtmlResponse.java => HtmlResponse.java} | 4 +-- .../http/InvalidRequestException.java | 4 +-- ...ectResponse.java => RedirectResponse.java} | 4 +-- .../tinyoauth2client/http/RedirectTarget.java | 16 +++++------ .../http/{HttpResponse.java => Response.java} | 14 +++++----- ...sponseTest.java => EmptyResponseTest.java} | 4 +-- ...esponseTest.java => HtmlResponseTest.java} | 4 +-- .../http/HttpResponseTest.java | 28 ------------------- ...nseTest.java => RedirectResponseTest.java} | 4 +-- .../tinyoauth2client/http/ResponseTest.java | 28 +++++++++++++++++++ 11 files changed, 57 insertions(+), 57 deletions(-) rename src/main/java/io/github/coffeelibs/tinyoauth2client/http/{HttpEmptyResponse.java => EmptyResponse.java} (77%) rename src/main/java/io/github/coffeelibs/tinyoauth2client/http/{HttpHtmlResponse.java => HtmlResponse.java} (86%) rename src/main/java/io/github/coffeelibs/tinyoauth2client/http/{HttpRedirectResponse.java => RedirectResponse.java} (81%) rename src/main/java/io/github/coffeelibs/tinyoauth2client/http/{HttpResponse.java => Response.java} (62%) rename src/test/java/io/github/coffeelibs/tinyoauth2client/http/{HttpEmptyResponseTest.java => EmptyResponseTest.java} (85%) rename src/test/java/io/github/coffeelibs/tinyoauth2client/http/{HttpHtmlResponseTest.java => HtmlResponseTest.java} (86%) delete mode 100644 src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpResponseTest.java rename src/test/java/io/github/coffeelibs/tinyoauth2client/http/{HttpRedirectResponseTest.java => RedirectResponseTest.java} (77%) create mode 100644 src/test/java/io/github/coffeelibs/tinyoauth2client/http/ResponseTest.java diff --git a/src/main/java/io/github/coffeelibs/tinyoauth2client/http/HttpEmptyResponse.java b/src/main/java/io/github/coffeelibs/tinyoauth2client/http/EmptyResponse.java similarity index 77% rename from src/main/java/io/github/coffeelibs/tinyoauth2client/http/HttpEmptyResponse.java rename to src/main/java/io/github/coffeelibs/tinyoauth2client/http/EmptyResponse.java index a10cfa6..1b0708c 100644 --- a/src/main/java/io/github/coffeelibs/tinyoauth2client/http/HttpEmptyResponse.java +++ b/src/main/java/io/github/coffeelibs/tinyoauth2client/http/EmptyResponse.java @@ -3,11 +3,11 @@ import java.io.IOException; import java.io.Writer; -class HttpEmptyResponse implements HttpResponse { +class EmptyResponse implements Response { private final Status status; - public HttpEmptyResponse(HttpResponse.Status status) { + public EmptyResponse(Response.Status status) { this.status = status; } diff --git a/src/main/java/io/github/coffeelibs/tinyoauth2client/http/HttpHtmlResponse.java b/src/main/java/io/github/coffeelibs/tinyoauth2client/http/HtmlResponse.java similarity index 86% rename from src/main/java/io/github/coffeelibs/tinyoauth2client/http/HttpHtmlResponse.java rename to src/main/java/io/github/coffeelibs/tinyoauth2client/http/HtmlResponse.java index a164f78..0af01e5 100644 --- a/src/main/java/io/github/coffeelibs/tinyoauth2client/http/HttpHtmlResponse.java +++ b/src/main/java/io/github/coffeelibs/tinyoauth2client/http/HtmlResponse.java @@ -4,12 +4,12 @@ import java.io.Writer; import java.nio.charset.StandardCharsets; -class HttpHtmlResponse implements HttpResponse { +class HtmlResponse implements Response { private final Status status; private final String body; - public HttpHtmlResponse(Status status, String body) { + public HtmlResponse(Status status, String body) { this.status = status; this.body = body; } diff --git a/src/main/java/io/github/coffeelibs/tinyoauth2client/http/InvalidRequestException.java b/src/main/java/io/github/coffeelibs/tinyoauth2client/http/InvalidRequestException.java index ecac84b..86eefc6 100644 --- a/src/main/java/io/github/coffeelibs/tinyoauth2client/http/InvalidRequestException.java +++ b/src/main/java/io/github/coffeelibs/tinyoauth2client/http/InvalidRequestException.java @@ -1,9 +1,9 @@ package io.github.coffeelibs.tinyoauth2client.http; public class InvalidRequestException extends Exception { - public final HttpResponse suggestedResponse; + public final Response suggestedResponse; - public InvalidRequestException(HttpResponse suggestedResponse) { + public InvalidRequestException(Response suggestedResponse) { this.suggestedResponse = suggestedResponse; } diff --git a/src/main/java/io/github/coffeelibs/tinyoauth2client/http/HttpRedirectResponse.java b/src/main/java/io/github/coffeelibs/tinyoauth2client/http/RedirectResponse.java similarity index 81% rename from src/main/java/io/github/coffeelibs/tinyoauth2client/http/HttpRedirectResponse.java rename to src/main/java/io/github/coffeelibs/tinyoauth2client/http/RedirectResponse.java index 5872ee9..fe14e82 100644 --- a/src/main/java/io/github/coffeelibs/tinyoauth2client/http/HttpRedirectResponse.java +++ b/src/main/java/io/github/coffeelibs/tinyoauth2client/http/RedirectResponse.java @@ -4,12 +4,12 @@ import java.io.Writer; import java.net.URI; -class HttpRedirectResponse implements HttpResponse { +class RedirectResponse implements Response { private final Status status; private URI target; - public HttpRedirectResponse(Status status, URI target) { + public RedirectResponse(Status status, URI target) { this.status = status; this.target = target; } diff --git a/src/main/java/io/github/coffeelibs/tinyoauth2client/http/RedirectTarget.java b/src/main/java/io/github/coffeelibs/tinyoauth2client/http/RedirectTarget.java index dfd1106..b9754c3 100644 --- a/src/main/java/io/github/coffeelibs/tinyoauth2client/http/RedirectTarget.java +++ b/src/main/java/io/github/coffeelibs/tinyoauth2client/http/RedirectTarget.java @@ -36,7 +36,7 @@ public class RedirectTarget implements Closeable { private final String path; private final String csrfToken; - private HttpResponse successResponse = HttpResponse.html(HttpResponse.Status.OK, "Success"); // TODO: allow customization with custom html or redirect + private Response successResponse = Response.html(Response.Status.OK, "Success"); // TODO: allow customization with custom html or redirect private RedirectTarget(ServerSocketChannel serverChannel, String path) { this.serverChannel = serverChannel; @@ -126,23 +126,23 @@ public String receive() throws IOException { throw new IOException("Unparseable Request", e); } if (!Path.of(path).equals(Path.of(requestUri.getPath()))) { - HttpResponse.empty(HttpResponse.Status.NOT_FOUND).write(writer); + Response.empty(Response.Status.NOT_FOUND).write(writer); throw new IOException("Requested invalid path " + requestUri); } var params = URIUtil.parseQueryString(requestUri.getRawQuery()); if (!csrfToken.equals(params.get("state"))) { - HttpResponse.empty(HttpResponse.Status.BAD_REQUEST).write(writer); + Response.empty(Response.Status.BAD_REQUEST).write(writer); throw new IOException("Missing or invalid state token"); } else if (params.containsKey("error")) { var html = "" + params.get("error") + ""; - HttpResponse.html(HttpResponse.Status.OK, html).write(writer); + Response.html(Response.Status.OK, html).write(writer); throw new IOException("Authorization failed"); // TODO more specific exception containing the error code } else if (params.containsKey("code")) { successResponse.write(writer); return params.get("code"); } else { - HttpResponse.empty(HttpResponse.Status.BAD_REQUEST).write(writer); + Response.empty(Response.Status.BAD_REQUEST).write(writer); throw new IOException("Missing authorization code"); } } @@ -159,16 +159,16 @@ public String receive() throws IOException { static URI parseRequestLine(String requestLine) throws InvalidRequestException { var words = requestLine.split(" "); if (words.length < 3) { - throw new InvalidRequestException(HttpResponse.empty(HttpResponse.Status.BAD_REQUEST)); + throw new InvalidRequestException(Response.empty(Response.Status.BAD_REQUEST)); } var method = words[0]; if (!"GET".equals(method)) { - throw new InvalidRequestException(HttpResponse.empty(HttpResponse.Status.METHOD_NOT_ALLOWED)); + throw new InvalidRequestException(Response.empty(Response.Status.METHOD_NOT_ALLOWED)); } try { return new URI(words[1]); } catch (URISyntaxException e) { - throw new InvalidRequestException(HttpResponse.empty(HttpResponse.Status.BAD_REQUEST)); + throw new InvalidRequestException(Response.empty(Response.Status.BAD_REQUEST)); } } diff --git a/src/main/java/io/github/coffeelibs/tinyoauth2client/http/HttpResponse.java b/src/main/java/io/github/coffeelibs/tinyoauth2client/http/Response.java similarity index 62% rename from src/main/java/io/github/coffeelibs/tinyoauth2client/http/HttpResponse.java rename to src/main/java/io/github/coffeelibs/tinyoauth2client/http/Response.java index 283ecbe..97d7656 100644 --- a/src/main/java/io/github/coffeelibs/tinyoauth2client/http/HttpResponse.java +++ b/src/main/java/io/github/coffeelibs/tinyoauth2client/http/Response.java @@ -4,20 +4,20 @@ import java.io.Writer; import java.net.URI; -interface HttpResponse { +interface Response { void write(Writer writer) throws IOException; - static HttpResponse empty(Status status) { - return new HttpEmptyResponse(status); + static Response empty(Status status) { + return new EmptyResponse(status); } - static HttpResponse html(Status status, String body) { - return new HttpHtmlResponse(status, body); + static Response html(Status status, String body) { + return new HtmlResponse(status, body); } - static HttpResponse redirect(URI target) { - return new HttpRedirectResponse(Status.SEE_OTHER, target); + static Response redirect(URI target) { + return new RedirectResponse(Status.SEE_OTHER, target); } enum Status { diff --git a/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpEmptyResponseTest.java b/src/test/java/io/github/coffeelibs/tinyoauth2client/http/EmptyResponseTest.java similarity index 85% rename from src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpEmptyResponseTest.java rename to src/test/java/io/github/coffeelibs/tinyoauth2client/http/EmptyResponseTest.java index 3cbb508..93895e8 100644 --- a/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpEmptyResponseTest.java +++ b/src/test/java/io/github/coffeelibs/tinyoauth2client/http/EmptyResponseTest.java @@ -6,12 +6,12 @@ import java.io.CharArrayWriter; import java.io.IOException; -public class HttpEmptyResponseTest { +public class EmptyResponseTest { @Test public void testWrite() throws IOException { var html = "Hello World"; - var response = new HttpHtmlResponse(HttpResponse.Status.OK, html); + var response = new HtmlResponse(Response.Status.OK, html); var writer = new CharArrayWriter(); response.write(writer); diff --git a/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpHtmlResponseTest.java b/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HtmlResponseTest.java similarity index 86% rename from src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpHtmlResponseTest.java rename to src/test/java/io/github/coffeelibs/tinyoauth2client/http/HtmlResponseTest.java index d50e12f..cc49789 100644 --- a/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpHtmlResponseTest.java +++ b/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HtmlResponseTest.java @@ -6,12 +6,12 @@ import java.io.CharArrayWriter; import java.io.IOException; -public class HttpHtmlResponseTest { +public class HtmlResponseTest { @Test public void testWrite() throws IOException { var body = "Hello World"; - var response = new HttpHtmlResponse(HttpResponse.Status.OK, body); + var response = new HtmlResponse(Response.Status.OK, body); var writer = new CharArrayWriter(); response.write(writer); diff --git a/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpResponseTest.java b/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpResponseTest.java deleted file mode 100644 index eb7c795..0000000 --- a/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpResponseTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package io.github.coffeelibs.tinyoauth2client.http; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.net.URI; - -public class HttpResponseTest { - - @Test - public void testEmpty() { - var result = HttpResponse.empty(HttpResponse.Status.OK); - Assertions.assertInstanceOf(HttpEmptyResponse.class, result); - } - - @Test - public void testHtml() { - var result = HttpResponse.html(HttpResponse.Status.OK, "test"); - Assertions.assertInstanceOf(HttpHtmlResponse.class, result); - } - - @Test - public void testRedirect() { - var result = HttpResponse.redirect(URI.create("http://google.com")); - Assertions.assertInstanceOf(HttpRedirectResponse.class, result); - } - -} \ No newline at end of file diff --git a/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpRedirectResponseTest.java b/src/test/java/io/github/coffeelibs/tinyoauth2client/http/RedirectResponseTest.java similarity index 77% rename from src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpRedirectResponseTest.java rename to src/test/java/io/github/coffeelibs/tinyoauth2client/http/RedirectResponseTest.java index 30df4e8..b5b2f96 100644 --- a/src/test/java/io/github/coffeelibs/tinyoauth2client/http/HttpRedirectResponseTest.java +++ b/src/test/java/io/github/coffeelibs/tinyoauth2client/http/RedirectResponseTest.java @@ -7,11 +7,11 @@ import java.io.IOException; import java.net.URI; -public class HttpRedirectResponseTest { +public class RedirectResponseTest { @Test public void testWrite() throws IOException { - var response = new HttpRedirectResponse(HttpResponse.Status.SEE_OTHER, URI.create("http://google.com")); + var response = new RedirectResponse(Response.Status.SEE_OTHER, URI.create("http://google.com")); var writer = new CharArrayWriter(); response.write(writer); diff --git a/src/test/java/io/github/coffeelibs/tinyoauth2client/http/ResponseTest.java b/src/test/java/io/github/coffeelibs/tinyoauth2client/http/ResponseTest.java new file mode 100644 index 0000000..75682b9 --- /dev/null +++ b/src/test/java/io/github/coffeelibs/tinyoauth2client/http/ResponseTest.java @@ -0,0 +1,28 @@ +package io.github.coffeelibs.tinyoauth2client.http; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.net.URI; + +public class ResponseTest { + + @Test + public void testEmpty() { + var result = Response.empty(Response.Status.OK); + Assertions.assertInstanceOf(EmptyResponse.class, result); + } + + @Test + public void testHtml() { + var result = Response.html(Response.Status.OK, "test"); + Assertions.assertInstanceOf(HtmlResponse.class, result); + } + + @Test + public void testRedirect() { + var result = Response.redirect(URI.create("http://google.com")); + Assertions.assertInstanceOf(RedirectResponse.class, result); + } + +} \ No newline at end of file From a1f5510d426c8e750e226b7e625ad8f2d4cea1ec Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Thu, 28 Apr 2022 11:27:32 +0200 Subject: [PATCH 5/9] basic support for custom error/success response pages --- .idea/inspectionProfiles/Project_Default.xml | 3 - .../coffeelibs/tinyoauth2client/AuthFlow.java | 64 ++++++++++++- .../tinyoauth2client/http/RedirectTarget.java | 16 +++- .../tinyoauth2client/http/Response.java | 2 +- .../tinyoauth2client/AuthFlowTest.java | 93 ++++++++++++++++--- 5 files changed, 155 insertions(+), 23 deletions(-) diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 282c7ac..146ab09 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -1,9 +1,6 @@