Ralph
Ralph

Reputation: 4868

How to use EL in extraParameters attribute of the @OpenIdAuthenticationMechanismDefinition?

I have a problem with defining parameters for an @OpenIdAuthenticationMechanismDefinition annotation introduced in the jakarta-ee-security-api of Jakarta EE 10. My goal is to understand the correct usage of expression language.

See this example:

@OpenIdAuthenticationMechanismDefinition(
        clientId = "123",
        clientSecret = "xxx",
        extraParameters = { "audience=abc", "team=def" }
)
public class MyBean  {
   ...
}

In this example I set 3 params hard coded. The Jakrata EE jakarta-ee-security-api also supports a more dynamic way using the Expression Language and referring to a CDI bean providing such values:

@OpenIdAuthenticationMechanismDefinition(
        clientId = "${configBean.clientId}",
        clientSecret = "${configBean.clientSecret}",
        extraParameters = { "audience=abc", "team=def" }
)
public class MyBean  {
   ...
}

This works all fine. My problem in this example is the parameter extraParameters which is expecting a string array. I did not manage to set this param by a config CDI Bean like in the following example code:

@OpenIdAuthenticationMechanismDefinition(
        clientId = "${configBean.clientId}",
        clientSecret = "${configBean.clientSecret}",
        extraParameters = "${configBean.extraParameters}"
)
public class MyBean  {
   ...
}


@ApplicationScoped
@Named
public class ConfigBean implements Serializable {
    ...
    public String getClientId() {
        return clientId;
    }
    public String getClientSecret() {
        return clientSecret;
    }
    public String[] getExtraParameters() {
        return { "audience=abc", "team=def" };
    }
}

Running this code in Wildfly 27 causes the following exception:

jakarta.enterprise.inject.spi.DefinitionException: OpenIdAuthenticationMechanismDefinition.extraParameters() value '${configBean.extraParameters}' is not of the format key=value

The question is: How can I set the extraParameters with expression language?

As you can see here there a also extra parameters defined ending with the suffix 'expression' expecting a String. But also with this param I did not found a solution to set a value with EL.

Upvotes: 1

Views: 243

Answers (1)

Ondro Mihályi
Ondro Mihályi

Reputation: 7730

you can set the scope and extraParameters via an expression using the scopeExpression and extraParametersExpression attributes:

@OpenIdAuthenticationMechanismDefinition(
        clientId = "${configBean.clientId}",
        clientSecret = "${configBean.clientSecret}",
        scopeExpression = "${configBean.scope}",
        extraParametersExpression = "${configBean.extraParameters}"
)
public class MyBean  {
   ...
}

In attributes of type String, you can directly use an expression inside the String value. For other types, the annotation provides alternative attributes with the Expression suffix, which are of type String, which accept an expression. Because scope and extraParameters are String arrays and not plain Strings, you need to use the scopeExpression and extraParameters

Upvotes: 1

Related Questions