Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set expire_in in OAUTH 2.0?

Tags:

I am using OAuth 2.0 with spring for token generation and I want to set expire_in manually so token can expire as per my criteria. Any one help me?

This is my response:

{     access_token: "c7a6cb95-1506-40e7-87d1-ddef0a239f64"     token_type: "bearer"     expires_in: 43199     scope: "read" } 
like image 421
Jay Thakkar Avatar asked Jun 26 '13 04:06

Jay Thakkar


People also ask

How can I expire my OAuth token?

This can be done using the following steps: convert expires_in to an expire time (epoch, RFC-3339/ISO-8601 datetime, etc.) store the expire time. on each resource request, check the current time against the expire time and make a token refresh request before the resource request if the access_token has expired.

Does refresh token expire in OAuth2?

By default, access tokens are valid for 60 days and programmatic refresh tokens are valid for a year.


2 Answers

It can be set with a ClientBuilder obtained from a ClientDetailsServiceConfigurer.

@Configuration @EnableAuthorizationServer public class OAuth2Config extends AuthorizationServerConfigurerAdapter {      @Override     public void configure(ClientDetailsServiceConfigurer clients) throws Exception {         clients.inMemory()             .withClient("client")             .secret("secret")             .authorizedGrantTypes("authorization_code", "refresh_token", "password")             .scopes("app")             .accessTokenValiditySeconds(30);     }      // ... additional configuration } 

or directly on DefaultTokenServices depending on your need.

@Configuration @EnableAuthorizationServer public class OAuth2Config extends AuthorizationServerConfigurerAdapter {     @Override     public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {          // optionally here you could just get endpoints.getConsumerTokenService()         // and cast to DefaultTokenServices and just set values needed          DefaultTokenServices tokenServices = new DefaultTokenServices();         tokenServices.setTokenStore(endpoints.getTokenStore());         tokenServices.setSupportRefreshToken(true);         tokenServices.setClientDetailsService(endpoints.getClientDetailsService());         tokenServices.setTokenEnhancer(endpoints.getTokenEnhancer());         tokenServices.setAccessTokenValiditySeconds(60);          endpoints.tokenServices(tokenServices);                 } } 
like image 138
DeezCashews Avatar answered Nov 13 '22 06:11

DeezCashews


configure your oauth configuration changing your Bean TokenServices and setting accessTokenValiditySeconds property :

<bean id="tokenServices"     class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">     <property name="accessTokenValiditySeconds" value="1" />     <property name="tokenStore" ref="tokenStore" />     <property name="supportRefreshToken" value="true" />     <property name="clientDetailsService" ref="clientDetails" /> </bean> 
like image 34
Clement Martino Avatar answered Nov 13 '22 06:11

Clement Martino