Spring OAuth2 Making `state` param at least 32 characters long












2















I am attempting to authorize against an external identity provider. Everything seems setup fine, but I keep getting a validation error with my identity provider because the state parameter automatically tacked onto my authorization request is not long enough:



For example:
&state=uYG5DC



The requirements of my IDP say that this state param must be at least 32-characters long. How can I programmatically increase the size of this auto-generated number?



Even if I could generate this number myself, it is not possible to override with other methods I have seen suggested. The following attempt fails because my manual setting of ?state=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz is superceded by the autogenerated param placed after it during the actual request:




@Bean
public OAuth2ProtectedResourceDetails loginGovOpenId() {
AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails() {
@Override
public String getUserAuthorizationUri() {
return super.getUserAuthorizationUri() + "?state=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
}
};
details.setClientId(clientId);
details.setAccessTokenUri(accessTokenUri);
details.setUserAuthorizationUri(userAuthorizationUri);
details.setScope(Arrays.asList("openid", "email"));
details.setPreEstablishedRedirectUri(redirectUri);
details.setUseCurrentUri(true);
return details;
}



The 6-character setting seems to be set here, is there a way to override this?
https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/common/util/RandomValueStringGenerator.java










share|improve this question





























    2















    I am attempting to authorize against an external identity provider. Everything seems setup fine, but I keep getting a validation error with my identity provider because the state parameter automatically tacked onto my authorization request is not long enough:



    For example:
    &state=uYG5DC



    The requirements of my IDP say that this state param must be at least 32-characters long. How can I programmatically increase the size of this auto-generated number?



    Even if I could generate this number myself, it is not possible to override with other methods I have seen suggested. The following attempt fails because my manual setting of ?state=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz is superceded by the autogenerated param placed after it during the actual request:




    @Bean
    public OAuth2ProtectedResourceDetails loginGovOpenId() {
    AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails() {
    @Override
    public String getUserAuthorizationUri() {
    return super.getUserAuthorizationUri() + "?state=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    }
    };
    details.setClientId(clientId);
    details.setAccessTokenUri(accessTokenUri);
    details.setUserAuthorizationUri(userAuthorizationUri);
    details.setScope(Arrays.asList("openid", "email"));
    details.setPreEstablishedRedirectUri(redirectUri);
    details.setUseCurrentUri(true);
    return details;
    }



    The 6-character setting seems to be set here, is there a way to override this?
    https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/common/util/RandomValueStringGenerator.java










    share|improve this question



























      2












      2








      2








      I am attempting to authorize against an external identity provider. Everything seems setup fine, but I keep getting a validation error with my identity provider because the state parameter automatically tacked onto my authorization request is not long enough:



      For example:
      &state=uYG5DC



      The requirements of my IDP say that this state param must be at least 32-characters long. How can I programmatically increase the size of this auto-generated number?



      Even if I could generate this number myself, it is not possible to override with other methods I have seen suggested. The following attempt fails because my manual setting of ?state=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz is superceded by the autogenerated param placed after it during the actual request:




      @Bean
      public OAuth2ProtectedResourceDetails loginGovOpenId() {
      AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails() {
      @Override
      public String getUserAuthorizationUri() {
      return super.getUserAuthorizationUri() + "?state=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
      }
      };
      details.setClientId(clientId);
      details.setAccessTokenUri(accessTokenUri);
      details.setUserAuthorizationUri(userAuthorizationUri);
      details.setScope(Arrays.asList("openid", "email"));
      details.setPreEstablishedRedirectUri(redirectUri);
      details.setUseCurrentUri(true);
      return details;
      }



      The 6-character setting seems to be set here, is there a way to override this?
      https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/common/util/RandomValueStringGenerator.java










      share|improve this question
















      I am attempting to authorize against an external identity provider. Everything seems setup fine, but I keep getting a validation error with my identity provider because the state parameter automatically tacked onto my authorization request is not long enough:



      For example:
      &state=uYG5DC



      The requirements of my IDP say that this state param must be at least 32-characters long. How can I programmatically increase the size of this auto-generated number?



      Even if I could generate this number myself, it is not possible to override with other methods I have seen suggested. The following attempt fails because my manual setting of ?state=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz is superceded by the autogenerated param placed after it during the actual request:




      @Bean
      public OAuth2ProtectedResourceDetails loginGovOpenId() {
      AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails() {
      @Override
      public String getUserAuthorizationUri() {
      return super.getUserAuthorizationUri() + "?state=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
      }
      };
      details.setClientId(clientId);
      details.setAccessTokenUri(accessTokenUri);
      details.setUserAuthorizationUri(userAuthorizationUri);
      details.setScope(Arrays.asList("openid", "email"));
      details.setPreEstablishedRedirectUri(redirectUri);
      details.setUseCurrentUri(true);
      return details;
      }



      The 6-character setting seems to be set here, is there a way to override this?
      https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/common/util/RandomValueStringGenerator.java







      spring-security-oauth2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 0:51







      forgo

















      asked Nov 17 '18 at 0:32









      forgoforgo

      561414




      561414
























          1 Answer
          1






          active

          oldest

          votes


















          0














          With the help of this post:
          spring security StateKeyGenerator custom instance



          I was able to come up with a working solution.



          In my configuration class marked with these annotations:



          @Configuration
          @EnableOAuth2Client



          I configured the following beans:



              @Bean
          public OAuth2ProtectedResourceDetails loginGovOpenId() {
          AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
          AuthorizationCodeResourceDetails details = new
          details.setClientId(clientId);
          details.setClientSecret(clientSecret);
          details.setAccessTokenUri(accessTokenUri);
          details.setUserAuthorizationUri(userAuthorizationUri);
          details.setScope(Arrays.asList("openid", "email"));
          details.setPreEstablishedRedirectUri(redirectUri);
          details.setUseCurrentUri(true);
          return details;
          }

          @Bean
          public StateKeyGenerator stateKeyGenerator() {
          return new CustomStateKeyGenerator();
          }

          @Bean
          public AccessTokenProvider accessTokenProvider() {
          AuthorizationCodeAccessTokenProvider accessTokenProvider = new AuthorizationCodeAccessTokenProvider();
          accessTokenProvider.setStateKeyGenerator(stateKeyGenerator());
          return accessTokenProvider;
          }

          @Bean
          public OAuth2RestTemplate loginGovOpenIdTemplate(final OAuth2ClientContext clientContext) {
          final OAuth2RestTemplate template = new OAuth2RestTemplate(loginGovOpenId(), clientContext);
          template.setAccessTokenProvider(accessTokenProvider());
          return template;
          }


          Where my CustomStateKeyGenerator implementation class looks as follows:



          public class CustomStateKeyGenerator implements StateKeyGenerator {

          // login.gov requires state to be at least 32-characters long
          private static int length = 32;
          private RandomValueStringGenerator generator = new RandomValueStringGenerator(length);

          @Override
          public String generateKey(OAuth2ProtectedResourceDetails resource) {
          return generator.generate();
          }
          }





          share|improve this answer
























          • Keep in mind, with the latest Spring Security 5 oauth2Login() implementation, I did not need to make this custom key generator as they appear to have increased the default size of the state parameter beyond 32-characters.

            – forgo
            Nov 23 '18 at 21:37











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347087%2fspring-oauth2-making-state-param-at-least-32-characters-long%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          With the help of this post:
          spring security StateKeyGenerator custom instance



          I was able to come up with a working solution.



          In my configuration class marked with these annotations:



          @Configuration
          @EnableOAuth2Client



          I configured the following beans:



              @Bean
          public OAuth2ProtectedResourceDetails loginGovOpenId() {
          AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
          AuthorizationCodeResourceDetails details = new
          details.setClientId(clientId);
          details.setClientSecret(clientSecret);
          details.setAccessTokenUri(accessTokenUri);
          details.setUserAuthorizationUri(userAuthorizationUri);
          details.setScope(Arrays.asList("openid", "email"));
          details.setPreEstablishedRedirectUri(redirectUri);
          details.setUseCurrentUri(true);
          return details;
          }

          @Bean
          public StateKeyGenerator stateKeyGenerator() {
          return new CustomStateKeyGenerator();
          }

          @Bean
          public AccessTokenProvider accessTokenProvider() {
          AuthorizationCodeAccessTokenProvider accessTokenProvider = new AuthorizationCodeAccessTokenProvider();
          accessTokenProvider.setStateKeyGenerator(stateKeyGenerator());
          return accessTokenProvider;
          }

          @Bean
          public OAuth2RestTemplate loginGovOpenIdTemplate(final OAuth2ClientContext clientContext) {
          final OAuth2RestTemplate template = new OAuth2RestTemplate(loginGovOpenId(), clientContext);
          template.setAccessTokenProvider(accessTokenProvider());
          return template;
          }


          Where my CustomStateKeyGenerator implementation class looks as follows:



          public class CustomStateKeyGenerator implements StateKeyGenerator {

          // login.gov requires state to be at least 32-characters long
          private static int length = 32;
          private RandomValueStringGenerator generator = new RandomValueStringGenerator(length);

          @Override
          public String generateKey(OAuth2ProtectedResourceDetails resource) {
          return generator.generate();
          }
          }





          share|improve this answer
























          • Keep in mind, with the latest Spring Security 5 oauth2Login() implementation, I did not need to make this custom key generator as they appear to have increased the default size of the state parameter beyond 32-characters.

            – forgo
            Nov 23 '18 at 21:37
















          0














          With the help of this post:
          spring security StateKeyGenerator custom instance



          I was able to come up with a working solution.



          In my configuration class marked with these annotations:



          @Configuration
          @EnableOAuth2Client



          I configured the following beans:



              @Bean
          public OAuth2ProtectedResourceDetails loginGovOpenId() {
          AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
          AuthorizationCodeResourceDetails details = new
          details.setClientId(clientId);
          details.setClientSecret(clientSecret);
          details.setAccessTokenUri(accessTokenUri);
          details.setUserAuthorizationUri(userAuthorizationUri);
          details.setScope(Arrays.asList("openid", "email"));
          details.setPreEstablishedRedirectUri(redirectUri);
          details.setUseCurrentUri(true);
          return details;
          }

          @Bean
          public StateKeyGenerator stateKeyGenerator() {
          return new CustomStateKeyGenerator();
          }

          @Bean
          public AccessTokenProvider accessTokenProvider() {
          AuthorizationCodeAccessTokenProvider accessTokenProvider = new AuthorizationCodeAccessTokenProvider();
          accessTokenProvider.setStateKeyGenerator(stateKeyGenerator());
          return accessTokenProvider;
          }

          @Bean
          public OAuth2RestTemplate loginGovOpenIdTemplate(final OAuth2ClientContext clientContext) {
          final OAuth2RestTemplate template = new OAuth2RestTemplate(loginGovOpenId(), clientContext);
          template.setAccessTokenProvider(accessTokenProvider());
          return template;
          }


          Where my CustomStateKeyGenerator implementation class looks as follows:



          public class CustomStateKeyGenerator implements StateKeyGenerator {

          // login.gov requires state to be at least 32-characters long
          private static int length = 32;
          private RandomValueStringGenerator generator = new RandomValueStringGenerator(length);

          @Override
          public String generateKey(OAuth2ProtectedResourceDetails resource) {
          return generator.generate();
          }
          }





          share|improve this answer
























          • Keep in mind, with the latest Spring Security 5 oauth2Login() implementation, I did not need to make this custom key generator as they appear to have increased the default size of the state parameter beyond 32-characters.

            – forgo
            Nov 23 '18 at 21:37














          0












          0








          0







          With the help of this post:
          spring security StateKeyGenerator custom instance



          I was able to come up with a working solution.



          In my configuration class marked with these annotations:



          @Configuration
          @EnableOAuth2Client



          I configured the following beans:



              @Bean
          public OAuth2ProtectedResourceDetails loginGovOpenId() {
          AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
          AuthorizationCodeResourceDetails details = new
          details.setClientId(clientId);
          details.setClientSecret(clientSecret);
          details.setAccessTokenUri(accessTokenUri);
          details.setUserAuthorizationUri(userAuthorizationUri);
          details.setScope(Arrays.asList("openid", "email"));
          details.setPreEstablishedRedirectUri(redirectUri);
          details.setUseCurrentUri(true);
          return details;
          }

          @Bean
          public StateKeyGenerator stateKeyGenerator() {
          return new CustomStateKeyGenerator();
          }

          @Bean
          public AccessTokenProvider accessTokenProvider() {
          AuthorizationCodeAccessTokenProvider accessTokenProvider = new AuthorizationCodeAccessTokenProvider();
          accessTokenProvider.setStateKeyGenerator(stateKeyGenerator());
          return accessTokenProvider;
          }

          @Bean
          public OAuth2RestTemplate loginGovOpenIdTemplate(final OAuth2ClientContext clientContext) {
          final OAuth2RestTemplate template = new OAuth2RestTemplate(loginGovOpenId(), clientContext);
          template.setAccessTokenProvider(accessTokenProvider());
          return template;
          }


          Where my CustomStateKeyGenerator implementation class looks as follows:



          public class CustomStateKeyGenerator implements StateKeyGenerator {

          // login.gov requires state to be at least 32-characters long
          private static int length = 32;
          private RandomValueStringGenerator generator = new RandomValueStringGenerator(length);

          @Override
          public String generateKey(OAuth2ProtectedResourceDetails resource) {
          return generator.generate();
          }
          }





          share|improve this answer













          With the help of this post:
          spring security StateKeyGenerator custom instance



          I was able to come up with a working solution.



          In my configuration class marked with these annotations:



          @Configuration
          @EnableOAuth2Client



          I configured the following beans:



              @Bean
          public OAuth2ProtectedResourceDetails loginGovOpenId() {
          AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
          AuthorizationCodeResourceDetails details = new
          details.setClientId(clientId);
          details.setClientSecret(clientSecret);
          details.setAccessTokenUri(accessTokenUri);
          details.setUserAuthorizationUri(userAuthorizationUri);
          details.setScope(Arrays.asList("openid", "email"));
          details.setPreEstablishedRedirectUri(redirectUri);
          details.setUseCurrentUri(true);
          return details;
          }

          @Bean
          public StateKeyGenerator stateKeyGenerator() {
          return new CustomStateKeyGenerator();
          }

          @Bean
          public AccessTokenProvider accessTokenProvider() {
          AuthorizationCodeAccessTokenProvider accessTokenProvider = new AuthorizationCodeAccessTokenProvider();
          accessTokenProvider.setStateKeyGenerator(stateKeyGenerator());
          return accessTokenProvider;
          }

          @Bean
          public OAuth2RestTemplate loginGovOpenIdTemplate(final OAuth2ClientContext clientContext) {
          final OAuth2RestTemplate template = new OAuth2RestTemplate(loginGovOpenId(), clientContext);
          template.setAccessTokenProvider(accessTokenProvider());
          return template;
          }


          Where my CustomStateKeyGenerator implementation class looks as follows:



          public class CustomStateKeyGenerator implements StateKeyGenerator {

          // login.gov requires state to be at least 32-characters long
          private static int length = 32;
          private RandomValueStringGenerator generator = new RandomValueStringGenerator(length);

          @Override
          public String generateKey(OAuth2ProtectedResourceDetails resource) {
          return generator.generate();
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 20:32









          forgoforgo

          561414




          561414













          • Keep in mind, with the latest Spring Security 5 oauth2Login() implementation, I did not need to make this custom key generator as they appear to have increased the default size of the state parameter beyond 32-characters.

            – forgo
            Nov 23 '18 at 21:37



















          • Keep in mind, with the latest Spring Security 5 oauth2Login() implementation, I did not need to make this custom key generator as they appear to have increased the default size of the state parameter beyond 32-characters.

            – forgo
            Nov 23 '18 at 21:37

















          Keep in mind, with the latest Spring Security 5 oauth2Login() implementation, I did not need to make this custom key generator as they appear to have increased the default size of the state parameter beyond 32-characters.

          – forgo
          Nov 23 '18 at 21:37





          Keep in mind, with the latest Spring Security 5 oauth2Login() implementation, I did not need to make this custom key generator as they appear to have increased the default size of the state parameter beyond 32-characters.

          – forgo
          Nov 23 '18 at 21:37


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347087%2fspring-oauth2-making-state-param-at-least-32-characters-long%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory