-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add OAuth provider configuration and restricted authentic…
…ation profile - introduce OAuthProviderConfig and OAuthProvidersConfig for handling OAuth configurations. - add RestrictedAuthenticationProfile for testing authentication with disabled sign-up. - implement OAuthResource for handling OAuth login and callback flows. - update ApplicationConfig and UserResource to respect sign-up enabled config. - enhance Javadocs for various domain features.
- Loading branch information
1 parent
a3ac13d
commit 78247c7
Showing
16 changed files
with
276 additions
and
7 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
56 changes: 56 additions & 0 deletions
56
src/main/java/dev/cloudeko/zenei/application/web/resource/OAuthResource.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,56 @@ | ||
package dev.cloudeko.zenei.application.web.resource; | ||
|
||
import dev.cloudeko.zenei.infrastructure.config.ApplicationConfig; | ||
import dev.cloudeko.zenei.infrastructure.config.OAuthProviderConfig; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.PathParam; | ||
import jakarta.ws.rs.QueryParam; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.UriBuilder; | ||
import lombok.AllArgsConstructor; | ||
import org.eclipse.microprofile.openapi.annotations.tags.Tag; | ||
|
||
@Path("/oauth") | ||
@AllArgsConstructor | ||
@Tag(name = "OAuth Service", description = "OAuth service used for authentication") | ||
public class OAuthResource { | ||
|
||
private final ApplicationConfig config; | ||
|
||
@GET | ||
@Path("/login/{provider}") | ||
public Response login(@PathParam("provider") String provider) { | ||
final var providerConfig = getProviderConfig(provider); | ||
|
||
if (providerConfig == null) { | ||
return Response.status(Response.Status.NOT_FOUND).build(); | ||
} | ||
|
||
final var uriBuilder = UriBuilder.fromUri(providerConfig.authorizationUri()) | ||
.queryParam("client_id", providerConfig.clientId()) | ||
.queryParam("redirect_uri", providerConfig.redirectUri()) | ||
.queryParam("response_type", "code") | ||
.queryParam("scope", "openid profile email"); | ||
|
||
return Response.temporaryRedirect(uriBuilder.build()).build(); | ||
} | ||
|
||
@GET | ||
@Path("/callback/{provider}") | ||
public Response callback(@PathParam("provider") String provider, | ||
@QueryParam("code") String code, | ||
@QueryParam("state") String state) { | ||
final var providerConfig = getProviderConfig(provider); | ||
|
||
if (providerConfig == null) { | ||
return Response.status(Response.Status.NOT_FOUND).build(); | ||
} | ||
|
||
return Response.ok("Received auth code: " + code).build(); | ||
} | ||
|
||
private OAuthProviderConfig getProviderConfig(String provider) { | ||
return config.getOAuthProvidersConfig().providers().get(provider); | ||
} | ||
} |
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
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
18 changes: 18 additions & 0 deletions
18
src/main/java/dev/cloudeko/zenei/infrastructure/config/OAuthProviderConfig.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,18 @@ | ||
package dev.cloudeko.zenei.infrastructure.config; | ||
|
||
public interface OAuthProviderConfig { | ||
|
||
String clientId(); | ||
|
||
String clientSecret(); | ||
|
||
String authorizationUri(); | ||
|
||
String tokenUri(); | ||
|
||
String userInfoUri(); | ||
|
||
String redirectUri(); | ||
|
||
String scope(); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/dev/cloudeko/zenei/infrastructure/config/OAuthProvidersConfig.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,13 @@ | ||
package dev.cloudeko.zenei.infrastructure.config; | ||
|
||
import io.smallrye.config.ConfigMapping; | ||
import io.smallrye.config.WithParentName; | ||
|
||
import java.util.Map; | ||
|
||
@ConfigMapping(prefix = "zenei.user.default") | ||
public interface OAuthProvidersConfig { | ||
|
||
@WithParentName | ||
Map<String, OAuthProviderConfig> providers(); | ||
} |
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 |
---|---|---|
|
@@ -35,4 +35,12 @@ zenei.jwt.issuer=https://example.com/issuer | |
zenei.user.default.admin.username=admin | ||
zenei.user.default.admin.email=[email protected] | ||
zenei.user.default.admin.password=test | ||
zenei.user.default.admin.role=admin | ||
zenei.user.default.admin.role=admin | ||
|
||
# Github OAuth | ||
#oauth.github.client-id=<your-client-id> | ||
#oauth.github.client-secret=<your-client-secret> | ||
#oauth.github.auth-uri=https://github.com/login/oauth/authorize | ||
#oauth.github.token-uri=https://github.com/login/oauth/access_token | ||
#oauth.github.user-info-uri=https://api.github.com/user | ||
#oauth.github.redirect-uri=http://localhost:8080/oauth/callback/github |
36 changes: 36 additions & 0 deletions
36
src/test/java/dev/cloudeko/zenei/auth/AuthenticationFlowWithDisabledSignupTest.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,36 @@ | ||
package dev.cloudeko.zenei.auth; | ||
|
||
import dev.cloudeko.zenei.profile.RestrictedAuthenticationProfile; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.TestProfile; | ||
import io.restassured.RestAssured; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import org.junit.jupiter.api.*; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
@QuarkusTest | ||
@TestProfile(RestrictedAuthenticationProfile.class) | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
public class AuthenticationFlowWithDisabledSignupTest { | ||
|
||
@BeforeAll | ||
static void setup() { | ||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); | ||
} | ||
|
||
@Test | ||
@DisplayName("Create user via email and password (POST /user) should return (403 FORBIDDEN)") | ||
void testCreateUser() { | ||
given() | ||
.contentType(MediaType.APPLICATION_FORM_URLENCODED) | ||
.formParam("username", "test-user2") | ||
.formParam("email", "[email protected]") | ||
.formParam("password", "test-password") | ||
.formParam("strategy", "PASSWORD") | ||
.post("/user") | ||
.then() | ||
.statusCode(Response.Status.FORBIDDEN.getStatusCode()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/java/dev/cloudeko/zenei/profile/RestrictedAuthenticationProfile.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,19 @@ | ||
package dev.cloudeko.zenei.profile; | ||
|
||
import io.quarkus.test.junit.QuarkusTestProfile; | ||
|
||
import java.util.Map; | ||
|
||
public class RestrictedAuthenticationProfile implements QuarkusTestProfile { | ||
|
||
@Override | ||
public Map<String, String> getConfigOverrides() { | ||
return Map.of( | ||
"zenei.auth.sign-up.enabled", "false", | ||
"zenei.user.default.admin.username", "admin", | ||
"zenei.user.default.admin.email", "[email protected]", | ||
"zenei.user.default.admin.password", "test", | ||
"zenei.user.default.admin.role", "admin" | ||
); | ||
} | ||
} |