-
Notifications
You must be signed in to change notification settings - Fork 942
Refactor messageContext initialization for enhanced scope availability in SamlAssertionConsumerFunction #5622
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left one nit but looks solid. Thanks @yeojin-dev ! 🙇 👍 🙇
@@ -56,14 +56,14 @@ attach it to your <type://Server>. | |||
```java | |||
// Specify information about your keystore. | |||
// The keystore contains two key pairs, which are identified as 'signing' and 'encryption'. | |||
KeyStoreCredentialResolver credentialResolver = | |||
CredentialResolver credentialResolver = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing this is a mistake since CredentialResolver
doesn't exist
CredentialResolver credentialResolver = | |
KeyStoreCredentialResolver credentialResolver = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I think we need to change this one too. :)
armeria/examples/saml-service-provider/src/main/java/example/armeria/server/saml/sp/Main.java
Line 55 in 3d800a2
final CredentialResolver credentialResolver = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops nevermind, I was looking at the wrong part of the code. The fix looks good 👍
.keyPassword("signing", "N5^X[hvG") | ||
.keyPassword("encryption", "N5^X[hvG") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 👍 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @yeojin-dev!
@@ -116,7 +117,7 @@ public HttpResponse serve(ServiceRequestContext ctx, AggregatedHttpRequest req, | |||
|
|||
return ssoHandler.loginSucceeded(ctx, req, messageContext, sessionIndex, relayState); | |||
} catch (SamlException e) { | |||
return ssoHandler.loginFailed(ctx, req, null, e); | |||
return ssoHandler.loginFailed(ctx, req, messageContext, e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add this messageContext to the log message?
armeria/saml/src/main/java/com/linecorp/armeria/server/saml/SamlServiceProviderBuilder.java
Line 129 in b2aa9f4
logger.warn("{} SAML SSO failed", ctx, cause); |
Could you also add a simple test for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll. 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating test cases for the use of messageContext
is challenging due to the internal use of static methods (HttpRedirectBindingUtil
and HttpPostBindingUtil
) in the serve()
function. While mocking static functions is possible, it is not a best practice and is not utilized by Armeria. To improve the design for easier testing, I propose the creation of SamlBindingProcessorFactory
and SamlBindingProcessor
interfaces. This structure allows for more flexible and maintainable code. Below is a brief example of how these interfaces could be implemented:
public interface SamlBindingProcessor {
MessageContext<Response> toSamlObject(AggregatedHttpRequest request, String responseType);
}
public interface SamlBindingProcessorFactory {
SamlBindingProcessor create(SamlBindingProtocol bindingProtocol);
}
public class DefaultSamlBindingProcessorFactory implements SamlBindingProcessorFactory {
@Override
public SamlBindingProcessor create(SamlBindingProtocol bindingProtocol) {
switch (bindingProtocol) {
case HTTP_REDIRECT:
return new HttpRedirectBindingProcessor();
case HTTP_POST:
return new HttpPostBindingProcessor();
default:
throw new IllegalArgumentException("Unsupported binding protocol");
}
}
}
public class SamlAssertionConsumerFunction {
private SamlBindingProcessor processor;
public SamlAssertionConsumerFunction(SamlAssertionConsumerConfig cfg, String entityId,
SamlBindingProcessorFactory processorFactory) {
this.processor = processorFactory.create(cfg.endpoint().bindingProtocol());
// Use the processor like this:
// MessageContext<Response> messageContext = processor.toSamlObject(req, SAML_RESPONSE);
}
}
These changes necessitate more extensive modifications than what is currently proposed in the PR. Please review and let me know if there are any misunderstandings or further adjustments needed. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I thought we could just use the existing test.
For example, we could probably capture the messageContext here and use it in the test where the login fails.
https://github.com/minwoox/armeria/blob/main/saml/src/test/java/com/linecorp/armeria/server/saml/SamlServiceProviderTest.java#L274
SamlBindingProcessorFactory
and SamlBindingProcessor
seems well-designed, but I'm uncertain whether customization is necessary. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yeojin-dev, I've added a test. 😉
8f87ec2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM once @minwoox's comment (add tests) is addressed. Thanks, @yeojin-dev!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot, @yeojin-dev! 😄
…y in SamlAssertionConsumerFunction (line#5622) Motivation: This PR addresses a code enhancement in the SamlAssertionConsumerFunction by initializing the messageContext variable at the beginning of the method. This change is intended to streamline the assignment and handling of messageContext, ensuring that it can be consistently used throughout the method, particularly in exception handling scenarios. Modifications: Declared messageContext at the method start to allow its use across the entire method scope, including within try and catch blocks. Result: - Closes line#5401 - The refactor ensures messageContext is available for error handling.
Motivation:
This PR addresses a code enhancement in the SamlAssertionConsumerFunction by initializing the messageContext variable at the beginning of the method. This change is intended to streamline the assignment and handling of messageContext, ensuring that it can be consistently used throughout the method, particularly in exception handling scenarios.
Modifications:
Declared messageContext at the method start to allow its use across the entire method scope, including within try and catch blocks.
Result: