@Test public void createOauthAccessTokenByGrantCode() throws Exception { Account user = savedAccountCitizen("user1"); AccountOnceToken oauthDirectAuthorizeToken = accountService.createOauthDirectAuthorizeToken(user); ClientApp clientApp = service.create(dev, "myapp", "ya ~ good", "http: String grantCode = service.directGrantCode(oauthDirectAuthorizeToken.getToken(), clientApp.getClientId(), "feed public", "http: OauthAccessTokenDto tokenDto = service.createOauthAccessTokenByGrantCode(grantCode, clientApp.getClientId(), "http: ClientAppUser appUser = service.listGrantedAppUsers(user).get(0); assertEquals(user.getAccountId(), appUser.getAccountId()); assertEquals(clientApp.getClientId(), appUser.getClientId()); assertEquals(clientApp.getClientSecret(), appUser.getCurrentClientSecret()); assertEquals(EnumSet.of(ClientAppScope.FEED, ClientAppScope.PUBLIC), appUser.getLastGrantedScopes()); ClientAppUserAccessToken clientAppUserAccessToken = service.verifyAccessToken(tokenDto.getAccessToken()) .get(); assertTrue(clientAppUserAccessToken.validate(appUser)); assertTrue(clientAppUserAccessToken.containsScope(ClientAppScope.FEED)); assertTrue(clientAppUserAccessToken.containsScope(ClientAppScope.PUBLIC)); }
@Override public OauthAccessTokenDto createOauthAccessTokenByGrantCode(String code, String clientId, String redirectUri) throws AccessDeniedException { return verifyRedirectUri(clientId, redirectUri).flatMap(clientApp -> { return GrantCode.tryDecode(code, oauthSecret) .filter(grantCode -> grantCode.matches(clientApp, redirectUri)) .map(grantCode -> createOauthAccessToken(clientApp, grantCode.getAccountId(), grantCode.getScopes(), ACCESS_TOKEN_EXPIRE_DURATION)); }).orElseThrow(() -> new AccessDeniedException("invalid grant for oauth access token")); }
ClientAppServiceImpl implements ClientAppService { @Override public OauthAccessTokenDto createOauthAccessTokenByGrantCode(String code, String clientId, String redirectUri) throws AccessDeniedException { return verifyRedirectUri(clientId, redirectUri).flatMap(clientApp -> { return GrantCode.tryDecode(code, oauthSecret) .filter(grantCode -> grantCode.matches(clientApp, redirectUri)) .map(grantCode -> createOauthAccessToken(clientApp, grantCode.getAccountId(), grantCode.getScopes(), ACCESS_TOKEN_EXPIRE_DURATION)); }).orElseThrow(() -> new AccessDeniedException("invalid grant for oauth access token")); } }
ClientAppServiceImpl implements ClientAppService { @Override public OauthAccessTokenDto createOauthAccessTokenByGrantCode(String code, String clientId, String redirectUri) throws AccessDeniedException { return verifyRedirectUri(clientId, redirectUri).flatMap(clientApp -> { return GrantCode.tryDecode(code, oauthSecret) .filter(grantCode -> grantCode.matches(clientApp, redirectUri)) .map(grantCode -> createOauthAccessToken(clientApp, grantCode.getAccountId(), grantCode.getScopes(), ACCESS_TOKEN_EXPIRE_DURATION)); }).orElseThrow(() -> new AccessDeniedException("invalid grant for oauth access token")); }  }
ClientAppServiceImpl implements ClientAppService { @Override public OauthAccessTokenDto createOauthAccessTokenByGrantCode(String code, String clientId, String redirectUri) throws AccessDeniedException { return verifyRedirectUri(clientId, redirectUri).flatMap(clientApp -> { return GrantCode.tryDecode(code, oauthSecret) .filter(grantCode -> grantCode.matches(clientApp, redirectUri)) .map(grantCode -> createOauthAccessToken(clientApp, grantCode.getAccountId(), grantCode.getScopes(), ACCESS_TOKEN_EXPIRE_DURATION)); }).orElseThrow(() -> new AccessDeniedException("invalid grant for oauth access token")); }  @Override ClientApp create(Authorization creator,
      String name,
      String description,
      String callbackUri); @Override ClientApp loadClientAppWithoutCache(String clientId); @Override List<ClientApp> listClientApps(Authorization creator); @Override void update(Authorization creator,
      String clientId,
      String name,
      String description,
      String callbackUri); @Override Optional<ClientApp> verifyRedirectUri(String clientId, String redirectUri); @Override String directGrantCode(String oauthDirectAuthorize,
      String clientId,
      String scope,
      String redirectUri); @Override OauthAccessTokenDto createOauthAccessTokenByGrantCode(String code,
      String clientId,
      String redirectUri); @Override Optional<ClientAppUserAccessToken> verifyAccessToken(@Nullable String rawAccessToken); @Override List<ClientAppUser> listGrantedAppUsers(Authorization authorization); @Override void resetClientAppSecret(Authorization creator, String clientId); @Override void revokeApp(Authorization user, String clientId); @Override boolean validateApp(String clientId, String clientSecret); @Override String generateDebugAccessToken(Authorization creator, String clientId); @Override List<ClientApp> listGrantedApps(Authorization user); }
ClientAppServiceImpl implements ClientAppService { @Override public OauthAccessTokenDto createOauthAccessTokenByGrantCode(String code, String clientId, String redirectUri) throws AccessDeniedException { return verifyRedirectUri(clientId, redirectUri).flatMap(clientApp -> { return GrantCode.tryDecode(code, oauthSecret) .filter(grantCode -> grantCode.matches(clientApp, redirectUri)) .map(grantCode -> createOauthAccessToken(clientApp, grantCode.getAccountId(), grantCode.getScopes(), ACCESS_TOKEN_EXPIRE_DURATION)); }).orElseThrow(() -> new AccessDeniedException("invalid grant for oauth access token")); }  @Override ClientApp create(Authorization creator,
      String name,
      String description,
      String callbackUri); @Override ClientApp loadClientAppWithoutCache(String clientId); @Override List<ClientApp> listClientApps(Authorization creator); @Override void update(Authorization creator,
      String clientId,
      String name,
      String description,
      String callbackUri); @Override Optional<ClientApp> verifyRedirectUri(String clientId, String redirectUri); @Override String directGrantCode(String oauthDirectAuthorize,
      String clientId,
      String scope,
      String redirectUri); @Override OauthAccessTokenDto createOauthAccessTokenByGrantCode(String code,
      String clientId,
      String redirectUri); @Override Optional<ClientAppUserAccessToken> verifyAccessToken(@Nullable String rawAccessToken); @Override List<ClientAppUser> listGrantedAppUsers(Authorization authorization); @Override void resetClientAppSecret(Authorization creator, String clientId); @Override void revokeApp(Authorization user, String clientId); @Override boolean validateApp(String clientId, String clientSecret); @Override String generateDebugAccessToken(Authorization creator, String clientId); @Override List<ClientApp> listGrantedApps(Authorization user);  }