-
Notifications
You must be signed in to change notification settings - Fork 146
Add Macaroon token support #2200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sdelamo
wants to merge
3
commits into
5.1.x
Choose a base branch
from
paperclip/mng-237-support-macaroons
base: 5.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,29 @@ | ||
| plugins { | ||
| id("io.micronaut.build.internal.security-module") | ||
| } | ||
|
|
||
| dependencies { | ||
| api(projects.micronautSecurity) | ||
| implementation(libs.managed.jmacaroons) | ||
|
|
||
| compileOnly(mn.micronaut.http.server) | ||
|
|
||
| testAnnotationProcessor(mn.micronaut.inject.java) | ||
| testImplementation(mnTest.micronaut.test.junit5) | ||
| testImplementation(mn.micronaut.http.client) | ||
| testImplementation(mn.micronaut.http.server.netty) | ||
| testImplementation(mnReactor.micronaut.reactor) | ||
| testImplementation(mnSerde.micronaut.serde.jackson) | ||
| testRuntimeOnly(mnLogging.logback.classic) | ||
| testRuntimeOnly(mnTest.junit.jupiter.engine) | ||
| } | ||
|
|
||
| tasks.withType<Test> { | ||
| useJUnitPlatform() | ||
| } | ||
|
|
||
| micronautBuild { | ||
| binaryCompatibility { | ||
| enabledAfter("5.1.0") | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
...main/java/io/micronaut/security/token/macaroons/DefaultMacaroonAuthenticationFactory.java
This file contains hidden or 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,48 @@ | ||
| /* | ||
| * Copyright 2017-2026 original authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micronaut.security.token.macaroons; | ||
|
|
||
| import io.micronaut.security.authentication.Authentication; | ||
| import io.micronaut.security.token.AbstractTokenAuthenticationFactory; | ||
| import io.micronaut.security.token.RolesFinder; | ||
| import io.micronaut.security.token.config.TokenConfiguration; | ||
| import jakarta.inject.Singleton; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Default {@link MacaroonAuthenticationFactory}. | ||
| * | ||
| * @author Sergio del Amo | ||
| * @since 5.1.0 | ||
| */ | ||
| @Singleton | ||
| public class DefaultMacaroonAuthenticationFactory extends AbstractTokenAuthenticationFactory<MacaroonAuthenticationContext> implements MacaroonAuthenticationFactory { | ||
|
|
||
| /** | ||
| * @param tokenConfiguration Token configuration | ||
| * @param rolesFinder Roles finder | ||
| */ | ||
| public DefaultMacaroonAuthenticationFactory(TokenConfiguration tokenConfiguration, | ||
| RolesFinder rolesFinder) { | ||
| super(tokenConfiguration, rolesFinder); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Authentication> createAuthentication(MacaroonAuthenticationContext context) { | ||
| return createAuthentication(context.getClaims()); | ||
| } | ||
| } |
137 changes: 137 additions & 0 deletions
137
...s/src/main/java/io/micronaut/security/token/macaroons/DefaultMacaroonClaimsGenerator.java
This file contains hidden or 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,137 @@ | ||
| /* | ||
| * Copyright 2017-2026 original authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micronaut.security.token.macaroons; | ||
|
|
||
| import io.micronaut.context.annotation.Requires; | ||
| import io.micronaut.context.annotation.Secondary; | ||
| import io.micronaut.context.env.Environment; | ||
| import io.micronaut.core.util.StringUtils; | ||
| import io.micronaut.runtime.ApplicationConfiguration; | ||
| import io.micronaut.security.authentication.Authentication; | ||
| import io.micronaut.security.token.Claims; | ||
| import io.micronaut.security.token.claims.ClaimsAudienceProvider; | ||
| import io.micronaut.security.token.claims.ClaimsGenerator; | ||
| import io.micronaut.security.token.claims.JtiGenerator; | ||
| import io.micronaut.security.token.config.TokenConfiguration; | ||
| import io.micronaut.security.token.config.TokenConfigurationProperties; | ||
| import jakarta.inject.Singleton; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.Arrays; | ||
| import java.util.Date; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Default claims generator used when no other {@link ClaimsGenerator} is available. | ||
| */ | ||
| @Requires(property = TokenConfigurationProperties.PREFIX + ".enabled", notEquals = StringUtils.FALSE) | ||
| @Requires(property = MacaroonConfigurationProperties.PREFIX + ".enabled", notEquals = StringUtils.FALSE) | ||
| @Requires(property = MacaroonConfigurationProperties.PREFIX + ".secret") | ||
| @Secondary | ||
| @Singleton | ||
| class DefaultMacaroonClaimsGenerator implements ClaimsGenerator { | ||
|
|
||
| private static final String ROLES_KEY = "rolesKey"; | ||
|
|
||
| private final TokenConfiguration tokenConfiguration; | ||
| @Nullable | ||
| private final JtiGenerator jtiGenerator; | ||
| @Nullable | ||
| private final ClaimsAudienceProvider claimsAudienceProvider; | ||
| private final String appName; | ||
|
|
||
| DefaultMacaroonClaimsGenerator(TokenConfiguration tokenConfiguration, | ||
| @Nullable JtiGenerator jtiGenerator, | ||
| @Nullable ClaimsAudienceProvider claimsAudienceProvider, | ||
| @Nullable ApplicationConfiguration applicationConfiguration) { | ||
| this.tokenConfiguration = tokenConfiguration; | ||
| this.jtiGenerator = jtiGenerator; | ||
| this.claimsAudienceProvider = claimsAudienceProvider; | ||
| this.appName = applicationConfiguration != null ? applicationConfiguration.getName().orElse(Environment.MICRONAUT) : Environment.MICRONAUT; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Object> generateClaims(Authentication authentication, @Nullable Integer expiration) { | ||
| Map<String, Object> claims = new LinkedHashMap<>(); | ||
| populateIat(claims); | ||
| populateExp(claims, expiration); | ||
| populateJti(claims); | ||
| populateIss(claims); | ||
| populateAud(claims); | ||
| populateNbf(claims); | ||
| populateWithAuthentication(claims, authentication); | ||
| return claims; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Object> generateClaimsSet(Map<String, ?> oldClaims, Integer expiration) { | ||
| Map<String, Object> claims = new LinkedHashMap<>(); | ||
| List<String> excludedClaims = Arrays.asList(Claims.EXPIRATION_TIME, Claims.ISSUED_AT, Claims.NOT_BEFORE); | ||
| oldClaims.forEach((key, value) -> { | ||
| if (!excludedClaims.contains(key)) { | ||
| claims.put(key, value); | ||
| } | ||
| }); | ||
| populateExp(claims, expiration); | ||
| populateIat(claims); | ||
| populateNbf(claims); | ||
| return claims; | ||
| } | ||
|
|
||
| private void populateIss(Map<String, Object> claims) { | ||
| claims.put(Claims.ISSUER, appName); | ||
| } | ||
|
|
||
| private void populateAud(Map<String, Object> claims) { | ||
| if (claimsAudienceProvider != null) { | ||
| claims.put(Claims.AUDIENCE, claimsAudienceProvider.audience()); | ||
| } | ||
| } | ||
|
|
||
| private void populateExp(Map<String, Object> claims, @Nullable Integer expiration) { | ||
| if (expiration != null) { | ||
| claims.put(Claims.EXPIRATION_TIME, Date.from(Instant.now().plus(expiration, ChronoUnit.SECONDS))); | ||
| } | ||
| } | ||
|
|
||
| private void populateNbf(Map<String, Object> claims) { | ||
| claims.put(Claims.NOT_BEFORE, new Date()); | ||
| } | ||
|
|
||
| private void populateIat(Map<String, Object> claims) { | ||
| claims.put(Claims.ISSUED_AT, new Date()); | ||
| } | ||
|
|
||
| private void populateJti(Map<String, Object> claims) { | ||
| if (jtiGenerator != null) { | ||
| claims.put(Claims.TOKEN_ID, jtiGenerator.generateJtiClaim()); | ||
| } | ||
| } | ||
|
|
||
| private void populateWithAuthentication(Map<String, Object> claims, Authentication authentication) { | ||
| claims.put(Claims.SUBJECT, authentication.getName()); | ||
| claims.putAll(authentication.getAttributes()); | ||
| String rolesKey = tokenConfiguration.getRolesName(); | ||
| if (!rolesKey.equalsIgnoreCase(TokenConfiguration.DEFAULT_ROLES_NAME)) { | ||
| claims.put(ROLES_KEY, rolesKey); | ||
| } | ||
| claims.put(rolesKey, authentication.getRoles()); | ||
| } | ||
| } | ||
88 changes: 88 additions & 0 deletions
88
...ns/src/main/java/io/micronaut/security/token/macaroons/MacaroonAuthenticationContext.java
This file contains hidden or 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,88 @@ | ||
| /* | ||
| * Copyright 2017-2026 original authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micronaut.security.token.macaroons; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Signature-verified Macaroon data used to build an authentication. | ||
| * | ||
| * @author Sergio del Amo | ||
| * @since 5.1.0 | ||
| */ | ||
| public final class MacaroonAuthenticationContext { | ||
|
Check warning on line 27 in security-macaroons/src/main/java/io/micronaut/security/token/macaroons/MacaroonAuthenticationContext.java
|
||
|
|
||
| private final String location; | ||
| private final String identifier; | ||
| private final MacaroonSerialization serialization; | ||
| private final Map<String, Object> claims; | ||
| private final List<MacaroonCaveat> caveats; | ||
|
|
||
| /** | ||
| * @param location The Macaroon location | ||
| * @param identifier The Macaroon identifier | ||
| * @param serialization The matched serialization | ||
| * @param claims Claims decoded from verified first-party caveats | ||
| * @param caveats First-party caveats | ||
| */ | ||
| public MacaroonAuthenticationContext(String location, | ||
| String identifier, | ||
| MacaroonSerialization serialization, | ||
| Map<String, Object> claims, | ||
| List<MacaroonCaveat> caveats) { | ||
| this.location = location; | ||
| this.identifier = identifier; | ||
| this.serialization = serialization; | ||
| this.claims = Map.copyOf(claims); | ||
| this.caveats = List.copyOf(caveats); | ||
| } | ||
|
|
||
| /** | ||
| * @return The Macaroon location | ||
| */ | ||
| public String getLocation() { | ||
| return location; | ||
| } | ||
|
|
||
| /** | ||
| * @return The Macaroon identifier | ||
| */ | ||
| public String getIdentifier() { | ||
| return identifier; | ||
| } | ||
|
|
||
| /** | ||
| * @return The matched serialization | ||
| */ | ||
| public MacaroonSerialization getSerialization() { | ||
| return serialization; | ||
| } | ||
|
|
||
| /** | ||
| * @return Claims decoded from verified first-party caveats | ||
| */ | ||
| public Map<String, Object> getClaims() { | ||
| return claims; | ||
| } | ||
|
|
||
| /** | ||
| * @return First-party caveats | ||
| */ | ||
| public List<MacaroonCaveat> getCaveats() { | ||
| return caveats; | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
...ns/src/main/java/io/micronaut/security/token/macaroons/MacaroonAuthenticationFactory.java
This file contains hidden or 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 @@ | ||
| /* | ||
| * Copyright 2017-2026 original authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micronaut.security.token.macaroons; | ||
|
|
||
| import io.micronaut.security.authentication.Authentication; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Creates an authentication from a verified Macaroon. | ||
| * | ||
| * @author Sergio del Amo | ||
| * @since 5.1.0 | ||
| */ | ||
| @FunctionalInterface | ||
| public interface MacaroonAuthenticationFactory { | ||
|
|
||
| /** | ||
| * @param context The verified Macaroon context | ||
| * @return An authentication if the verified Macaroon contains sufficient claims | ||
| */ | ||
| Optional<Authentication> createAuthentication(MacaroonAuthenticationContext context); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.