mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(signin): add secure branded consent flow (#2429)
This commit is contained in:
1 parent
1e443acd06
commit
09d7502b28
9 files changed
+804
-39
No files matched your search
+195
-2
@@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
@@ -28,7 +29,7 @@ class SigninIntegrationTest {
|
||||
private static final String REDIRECT_URI = "http://127.0.0.1:4567/oauth/callback";
|
||||
|
||||
@Test
|
||||
void authorizeRedirectsWithStateAndPkceCode() throws Exception {
|
||||
void authorizePresentsLocalConsentPageBeforeRedirectingWithCode() throws Exception {
|
||||
String verifier = verifier();
|
||||
String state = UUID.randomUUID().toString();
|
||||
|
||||
@@ -45,10 +46,61 @@ class SigninIntegrationTest {
|
||||
.get("/v1/authorize")
|
||||
.then()
|
||||
.statusCode(302)
|
||||
.header("Cache-Control", equalTo("no-store"))
|
||||
.extract()
|
||||
.header("Location");
|
||||
|
||||
URI redirect = URI.create(location);
|
||||
URI consent = URI.create(location);
|
||||
assertFalse(consent.isAbsolute());
|
||||
assertEquals("/_floci/signin/consent", consent.getPath());
|
||||
String page = request()
|
||||
.get(location)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("Cache-Control", equalTo("no-store"))
|
||||
.header("Referrer-Policy", equalTo("no-referrer"))
|
||||
.header("X-Content-Type-Options", equalTo("nosniff"))
|
||||
.header("Content-Security-Policy", equalTo("default-src 'none'; img-src 'self'; "
|
||||
+ "style-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'"))
|
||||
.extract()
|
||||
.asString();
|
||||
assertTrue(page.contains("Sign in to Floci"));
|
||||
assertTrue(page.contains("src=\"/_floci/signin/floci-header.svg\""));
|
||||
assertTrue(page.contains("alt=\"Floci\""));
|
||||
assertTrue(page.contains("href=\"/_floci/signin/floci.svg\""));
|
||||
assertTrue(page.contains("href=\"/_floci/signin/signin.css\""));
|
||||
assertTrue(page.contains("<form method=\"post\" action=\"/_floci/signin/consent\">"));
|
||||
String requestId = queryParams(consent.getRawQuery()).get("request_id");
|
||||
assertTrue(page.contains("name=\"request_id\" value=\"" + requestId + "\""));
|
||||
assertTrue(page.contains("name=\"action\" value=\"cancel\""));
|
||||
assertTrue(page.contains("name=\"action\" value=\"continue\""));
|
||||
assertTrue(page.contains("Continue"));
|
||||
|
||||
request()
|
||||
.get("/_floci/signin/floci-header.svg")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("Content-Type", org.hamcrest.Matchers.startsWith("image/svg+xml"))
|
||||
.body(org.hamcrest.Matchers.containsString("viewBox=\"0 0 531.25 156.71\""))
|
||||
.body(org.hamcrest.Matchers.containsString("#FF9900"));
|
||||
|
||||
request()
|
||||
.get("/_floci/signin/floci.svg")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("Content-Type", org.hamcrest.Matchers.startsWith("image/svg+xml"));
|
||||
|
||||
request()
|
||||
.get("/_floci/signin/signin.css")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("Content-Type", org.hamcrest.Matchers.startsWith("text/css"))
|
||||
.body(org.hamcrest.Matchers.containsString("--floci-brand: #5559a7"))
|
||||
.body(org.hamcrest.Matchers.containsString("--floci-orange: #ff9900"))
|
||||
.body(org.hamcrest.Matchers.containsString("border-color: #667085"));
|
||||
|
||||
String redirectLocation = approve(location);
|
||||
URI redirect = URI.create(redirectLocation);
|
||||
assertEquals(REDIRECT_URI, redirect.getScheme() + "://" + redirect.getAuthority() + redirect.getPath());
|
||||
Map<String, String> params = queryParams(redirect.getRawQuery());
|
||||
assertTrue(params.get("code").length() >= 1);
|
||||
@@ -74,6 +126,79 @@ class SigninIntegrationTest {
|
||||
.body("$", not(hasKey("__type")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void denyingConsentRedirectsWithAccessDenied() throws Exception {
|
||||
String state = UUID.randomUUID().toString();
|
||||
String consentLocation = request()
|
||||
.redirects().follow(false)
|
||||
.queryParam("client_id", CLIENT_ID)
|
||||
.queryParam("code_challenge", challenge(verifier()))
|
||||
.queryParam("code_challenge_method", "SHA-256")
|
||||
.queryParam("redirect_uri", REDIRECT_URI)
|
||||
.queryParam("response_type", "code")
|
||||
.queryParam("scope", "openid")
|
||||
.queryParam("state", state)
|
||||
.when()
|
||||
.get("/v1/authorize")
|
||||
.then()
|
||||
.statusCode(302)
|
||||
.extract()
|
||||
.header("Location");
|
||||
|
||||
URI consent = URI.create(consentLocation);
|
||||
String requestId = queryParams(consent.getRawQuery()).get("request_id");
|
||||
String redirectLocation = request()
|
||||
.redirects().follow(false)
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("request_id", requestId)
|
||||
.formParam("action", "cancel")
|
||||
.when()
|
||||
.post("/_floci/signin/consent")
|
||||
.then()
|
||||
.statusCode(302)
|
||||
.header("Cache-Control", equalTo("no-store"))
|
||||
.extract()
|
||||
.header("Location");
|
||||
|
||||
Map<String, String> params = queryParams(URI.create(redirectLocation).getRawQuery());
|
||||
assertEquals("access_denied", params.get("error"));
|
||||
assertEquals(state, params.get("state"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void consentRejectsMissingRequestIdAndUnknownAction() throws Exception {
|
||||
request()
|
||||
.get("/_floci/signin/consent")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.header("Cache-Control", equalTo("no-store"))
|
||||
.header("Referrer-Policy", equalTo("no-referrer"))
|
||||
.header("X-Content-Type-Options", equalTo("nosniff"))
|
||||
.header("Content-Security-Policy", equalTo("default-src 'none'; img-src 'self'; "
|
||||
+ "style-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'"))
|
||||
.body(org.hamcrest.Matchers.containsString("request_id is required"));
|
||||
|
||||
String verifier = verifier();
|
||||
String state = UUID.randomUUID().toString();
|
||||
String consentLocation = beginAuthorization(verifier, state);
|
||||
String requestId = queryParams(URI.create(consentLocation).getRawQuery()).get("request_id");
|
||||
|
||||
request()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("request_id", requestId)
|
||||
.formParam("action", "approve")
|
||||
.when()
|
||||
.post("/_floci/signin/consent")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(org.hamcrest.Matchers.containsString("action must be continue or cancel"));
|
||||
|
||||
String redirectLocation = approve(consentLocation);
|
||||
Map<String, String> params = queryParams(URI.create(redirectLocation).getRawQuery());
|
||||
assertTrue(params.containsKey("code"));
|
||||
assertEquals(state, params.get("state"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedTokenJsonUsesOauthErrorEnvelope() {
|
||||
request()
|
||||
@@ -178,6 +303,53 @@ class SigninIntegrationTest {
|
||||
"The authorization grant type is not supported by the authorization server."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void s3BucketNamedSigninDoesNotCollideWithConsentUi() {
|
||||
String objectBody = "S3 object under the signin bucket";
|
||||
String formerAssetBody = "S3 object at the former static asset path";
|
||||
request()
|
||||
.when()
|
||||
.put("/signin")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
try {
|
||||
request()
|
||||
.contentType("text/plain")
|
||||
.body(objectBody)
|
||||
.when()
|
||||
.put("/signin/consent")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
request()
|
||||
.when()
|
||||
.get("/signin/consent")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo(objectBody));
|
||||
|
||||
request()
|
||||
.contentType("text/plain")
|
||||
.body(formerAssetBody)
|
||||
.when()
|
||||
.put("/signin/signin.css")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
request()
|
||||
.when()
|
||||
.get("/signin/signin.css")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo(formerAssetBody));
|
||||
} finally {
|
||||
request().delete("/signin/signin.css").then().statusCode(204);
|
||||
request().delete("/signin/consent").then().statusCode(204);
|
||||
request().delete("/signin").then().statusCode(204);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorizationCodeExchangeReturnsTemporaryAwsCredentials() throws Exception {
|
||||
String verifier = verifier();
|
||||
@@ -308,6 +480,10 @@ class SigninIntegrationTest {
|
||||
}
|
||||
|
||||
private String authorize(String verifier, String state) throws Exception {
|
||||
return approve(beginAuthorization(verifier, state));
|
||||
}
|
||||
|
||||
private String beginAuthorization(String verifier, String state) throws Exception {
|
||||
return request()
|
||||
.redirects().follow(false)
|
||||
.queryParam("client_id", CLIENT_ID)
|
||||
@@ -325,6 +501,23 @@ class SigninIntegrationTest {
|
||||
.header("Location");
|
||||
}
|
||||
|
||||
private String approve(String consentLocation) {
|
||||
URI consent = URI.create(consentLocation);
|
||||
String requestId = queryParams(consent.getRawQuery()).get("request_id");
|
||||
return request()
|
||||
.redirects().follow(false)
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("request_id", requestId)
|
||||
.formParam("action", "continue")
|
||||
.when()
|
||||
.post("/_floci/signin/consent")
|
||||
.then()
|
||||
.statusCode(302)
|
||||
.header("Cache-Control", equalTo("no-store"))
|
||||
.extract()
|
||||
.header("Location");
|
||||
}
|
||||
|
||||
private RequestSpecification request() {
|
||||
return given();
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import io.github.hectorvent.floci.testing.MutableClock;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
@@ -79,11 +78,11 @@ class SigninServiceTest {
|
||||
@Test
|
||||
void requiresAwsSha256ChallengeMethod() throws Exception {
|
||||
SigninException error = assertThrows(SigninException.class,
|
||||
() -> service.authorize(CLIENT_ID, challenge(VERIFIER), "S256", REDIRECT_URI,
|
||||
() -> service.beginAuthorization(CLIENT_ID, challenge(VERIFIER), "S256", REDIRECT_URI,
|
||||
"code", "openid", "state", null));
|
||||
|
||||
assertEquals("invalid_request", error.error());
|
||||
service.authorize(CLIENT_ID, challenge(VERIFIER), "SHA-256", REDIRECT_URI,
|
||||
service.beginAuthorization(CLIENT_ID, challenge(VERIFIER), "SHA-256", REDIRECT_URI,
|
||||
"code", "openid", "state", null);
|
||||
}
|
||||
|
||||
@@ -137,9 +136,9 @@ class SigninServiceTest {
|
||||
REDIRECT_URI, VERIFIER, null, "r".repeat(2049)));
|
||||
assertTokenValidation(() -> service.exchange(CLIENT_ID, "refresh_token", null,
|
||||
null, null, "r".repeat(2049), null));
|
||||
assertInvalidRequest(() -> service.authorize(CLIENT_ID, challengeUnchecked(VERIFIER), "SHA-256",
|
||||
assertInvalidRequest(() -> service.beginAuthorization(CLIENT_ID, challengeUnchecked(VERIFIER), "SHA-256",
|
||||
REDIRECT_URI, "code", "openid", "state", ""));
|
||||
assertInvalidRequest(() -> service.authorize(CLIENT_ID, challengeUnchecked(VERIFIER), "SHA-256",
|
||||
assertInvalidRequest(() -> service.beginAuthorization(CLIENT_ID, challengeUnchecked(VERIFIER), "SHA-256",
|
||||
REDIRECT_URI, "code", "openid", "state", "r".repeat(2049)));
|
||||
|
||||
TokenResult result = service.exchange(CLIENT_ID, "authorization_code", code,
|
||||
@@ -183,6 +182,41 @@ class SigninServiceTest {
|
||||
assertTokenValidation(() -> exchangeCode(cleanedAfterCutoff, VERIFIER));
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorizationCodeLifetimeStartsAtConsentCompletion() throws Exception {
|
||||
String requestId = service.beginAuthorization(CLIENT_ID, challenge(VERIFIER), "SHA-256", REDIRECT_URI,
|
||||
"code", "openid", "state", null);
|
||||
clock.advance(Duration.ofMinutes(4));
|
||||
String code = query(service.completeAuthorization(requestId)).get("code");
|
||||
clock.advance(Duration.ofMinutes(4).plusSeconds(59));
|
||||
|
||||
TokenResult result = exchangeCode(code, VERIFIER);
|
||||
|
||||
assertEquals(900, result.expiresIn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void pendingAuthorizationRequestsAreSingleUse() throws Exception {
|
||||
String approvedRequest = service.beginAuthorization(CLIENT_ID, challenge(VERIFIER), "SHA-256", REDIRECT_URI,
|
||||
"code", "openid", "approved", null);
|
||||
service.completeAuthorization(approvedRequest);
|
||||
assertInvalidRequest(() -> service.completeAuthorization(approvedRequest));
|
||||
|
||||
String deniedRequest = service.beginAuthorization(CLIENT_ID, challenge(VERIFIER), "SHA-256", REDIRECT_URI,
|
||||
"code", "openid", "denied", null);
|
||||
service.denyAuthorization(deniedRequest);
|
||||
assertInvalidRequest(() -> service.denyAuthorization(deniedRequest));
|
||||
}
|
||||
|
||||
@Test
|
||||
void pendingAuthorizationRequestsExpireAtFiveMinuteBoundary() throws Exception {
|
||||
String requestId = service.beginAuthorization(CLIENT_ID, challenge(VERIFIER), "SHA-256", REDIRECT_URI,
|
||||
"code", "openid", "state", null);
|
||||
clock.advance(Duration.ofMinutes(5));
|
||||
|
||||
assertInvalidRequest(() -> service.completeAuthorization(requestId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsPkceMismatchWithoutRegisteringCredentials() throws Exception {
|
||||
String code = authorizeCode(VERIFIER);
|
||||
@@ -241,13 +275,14 @@ class SigninServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void rotatedRefreshTokenKeepsOriginalAbsoluteExpiry() throws Exception {
|
||||
void rotatedRefreshTokenFamilyKeepsOriginalAbsoluteExpiry() throws Exception {
|
||||
TokenResult initial = issueInitialTokens();
|
||||
clock.advance(Duration.ofHours(11).plusMinutes(59));
|
||||
|
||||
TokenResult rotated = refresh(initial.refreshToken());
|
||||
clock.advance(Duration.ofMinutes(1));
|
||||
|
||||
assertRefreshExpired(initial.refreshToken());
|
||||
assertRefreshExpired(rotated.refreshToken());
|
||||
}
|
||||
|
||||
@@ -289,6 +324,18 @@ class SigninServiceTest {
|
||||
assertFalse(error.modeled());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refreshGrantRetentionNeverOutlivesAbsoluteExpiry() {
|
||||
var expiresAt = clock.instant().plus(Duration.ofHours(12));
|
||||
var earlierReplayExpiry = expiresAt.minus(Duration.ofMinutes(1));
|
||||
var replayExpiresAt = expiresAt.plus(Duration.ofMinutes(14));
|
||||
|
||||
assertEquals(expiresAt, SigninService.refreshGrantRetentionExpiry(expiresAt, null));
|
||||
assertEquals(earlierReplayExpiry,
|
||||
SigninService.refreshGrantRetentionExpiry(expiresAt, earlierReplayExpiry));
|
||||
assertEquals(expiresAt, SigninService.refreshGrantRetentionExpiry(expiresAt, replayExpiresAt));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiryCleanupWaitsForInFlightRotation() throws Exception {
|
||||
TokenResult initial = issueInitialTokens();
|
||||
@@ -341,13 +388,13 @@ class SigninServiceTest {
|
||||
}
|
||||
|
||||
private String authorizeCode(String verifier) throws Exception {
|
||||
String redirect = service.authorize(CLIENT_ID, challenge(verifier), "SHA-256", REDIRECT_URI,
|
||||
String requestId = service.beginAuthorization(CLIENT_ID, challenge(verifier), "SHA-256", REDIRECT_URI,
|
||||
"code", "openid", "state", null);
|
||||
return query(URI.create(redirect).getRawQuery()).get("code");
|
||||
return query(service.completeAuthorization(requestId)).get("code");
|
||||
}
|
||||
|
||||
private String authorize(String codeChallenge, String clientId, String redirectUri, String state) {
|
||||
return service.authorize(clientId, codeChallenge, "SHA-256", redirectUri,
|
||||
return service.beginAuthorization(clientId, codeChallenge, "SHA-256", redirectUri,
|
||||
"code", "openid", state, null);
|
||||
}
|
||||
|
||||
@@ -418,7 +465,8 @@ class SigninServiceTest {
|
||||
return Base64.getUrlEncoder().withoutPadding().encodeToString(digest);
|
||||
}
|
||||
|
||||
private static Map<String, String> query(String query) {
|
||||
private static Map<String, String> query(String redirect) {
|
||||
String query = redirect.substring(redirect.indexOf('?') + 1);
|
||||
return java.util.Arrays.stream(query.split("&"))
|
||||
.map(pair -> pair.split("=", 2))
|
||||
.collect(java.util.stream.Collectors.toMap(
|
||||
|
||||
Reference in new issue
Block a user