DeviceTokenGranter.java

  1. /*******************************************************************************
  2.  * Copyright 2017 The MIT Internet Trust Consortium
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *******************************************************************************/

  16. package org.mitre.oauth2.token;

  17. import java.util.Date;

  18. import org.mitre.oauth2.exception.AuthorizationPendingException;
  19. import org.mitre.oauth2.exception.DeviceCodeExpiredException;
  20. import org.mitre.oauth2.model.DeviceCode;
  21. import org.mitre.oauth2.service.DeviceCodeService;
  22. import org.mitre.oauth2.web.DeviceEndpoint;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
  25. import org.springframework.security.oauth2.provider.ClientDetails;
  26. import org.springframework.security.oauth2.provider.ClientDetailsService;
  27. import org.springframework.security.oauth2.provider.OAuth2Authentication;
  28. import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
  29. import org.springframework.security.oauth2.provider.TokenRequest;
  30. import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
  31. import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
  32. import org.springframework.stereotype.Component;

  33. /**
  34.  * Implements https://tools.ietf.org/html/draft-ietf-oauth-device-flow
  35.  *
  36.  * @see DeviceEndpoint
  37.  *
  38.  * @author jricher
  39.  *
  40.  */
  41. @Component("deviceTokenGranter")
  42. public class DeviceTokenGranter extends AbstractTokenGranter {

  43.     public static final String GRANT_TYPE = "urn:ietf:params:oauth:grant-type:device_code";

  44.     @Autowired
  45.     private DeviceCodeService deviceCodeService;

  46.     /**
  47.      * @param tokenServices
  48.      * @param clientDetailsService
  49.      * @param requestFactory
  50.      * @param grantType
  51.      */
  52.     protected DeviceTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory) {
  53.         super(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE);
  54.     }

  55.     /* (non-Javadoc)
  56.      * @see org.springframework.security.oauth2.provider.token.AbstractTokenGranter#getOAuth2Authentication(org.springframework.security.oauth2.provider.ClientDetails, org.springframework.security.oauth2.provider.TokenRequest)
  57.      */
  58.     @Override
  59.     protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {

  60.         String deviceCode = tokenRequest.getRequestParameters().get("device_code");

  61.         // look up the device code and consume it
  62.         DeviceCode dc = deviceCodeService.findDeviceCode(deviceCode, client);

  63.         if (dc != null) {

  64.             // make sure the code hasn't expired yet
  65.             if (dc.getExpiration() != null && dc.getExpiration().before(new Date())) {
  66.                
  67.                 deviceCodeService.clearDeviceCode(deviceCode, client);
  68.                
  69.                 throw new DeviceCodeExpiredException("Device code has expired " + deviceCode);

  70.             } else if (!dc.isApproved()) {

  71.                 // still waiting for approval
  72.                 throw new AuthorizationPendingException("Authorization pending for code " + deviceCode);

  73.             } else {
  74.                 // inherit the (approved) scopes from the original request
  75.                 tokenRequest.setScope(dc.getScope());

  76.                 OAuth2Authentication auth = new OAuth2Authentication(getRequestFactory().createOAuth2Request(client, tokenRequest), dc.getAuthenticationHolder().getUserAuth());

  77.                 deviceCodeService.clearDeviceCode(deviceCode, client);
  78.                
  79.                 return auth;
  80.             }
  81.         } else {
  82.             throw new InvalidGrantException("Invalid device code: " + deviceCode);
  83.         }

  84.     }




  85. }