IdentityAuthorizationActivity implements AuthorizedClientActivity { @Override public Response execute(Authorization authorization, Client client, Request request, DateTime instant) { FindIdentityRequest findIdentityRequest = new FindIdentityRequest(authorization.identityId, GrantType.AUTHORIZATION_CODE, instant, authorization.params, authorization.clientId); Optional<Identity> possibleIdentity = identityFinder.findIdentity(findIdentityRequest); if (!possibleIdentity.isPresent()) { return OAuthError.invalidGrant("identity was not found"); } Identity identity = possibleIdentity.get(); return authorizedIdentityActivity.execute(client, identity, authorization.scopes, request, instant, authorization.params); } IdentityAuthorizationActivity(IdentityFinder identityFinder, AuthorizedIdentityActivity authorizedIdentityActivity); @Override Response execute(Authorization authorization, Client client, Request request, DateTime instant);  }
@Test public void happyPath() throws Exception { final Client anyClient = aNewClient().build(); final DateTime anyInstantTime = new DateTime(); final Authorization anyAuth = newAuthorization().withCodeChallenge(new CodeChallenge("::codeChallenge::", "::method::")).withCode("::any code::").withId("::user_id::").build(); final Identity identity = aNewIdentity().withId("::user_id::").build(); final Request request = aNewRequest().param("code", "::any code::").param("code_verifier", "::codeVerifier::").build(); context.checking(new Expectations() {{ oneOf(identityFinder).findIdentity(new FindIdentityRequest("::user_id::", GrantType.AUTHORIZATION_CODE, anyInstantTime, Maps.<String, String>newHashMap(), anyAuth.clientId)); will(returnValue(Optional.of(identity))); oneOf(authorizedIdentityActivity).execute(anyClient, identity, anyAuth.scopes, request, anyInstantTime, anyAuth.params); will(returnValue(new RsText("::response::"))); }}); Response response = identityAuthorizationActivity.execute(anyAuth, anyClient, request, anyInstantTime); assertThat(new RsPrint(response).printBody(), is(equalTo("::response::"))); }
@Test public void identityNotFound() throws Exception { final Client anyClient = aNewClient().build(); final DateTime anyInstantTime = new DateTime(); final Authorization anyAuth = newAuthorization().withCodeChallenge(new CodeChallenge("::codeChallenge::", "::method::")).withCode("::any code::").withId("::user_id::").build(); final Request request = aNewRequest().param("code", "::any code::").param("code_verifier", "::codeVerifier::").build(); context.checking(new Expectations() {{ oneOf(identityFinder).findIdentity(new FindIdentityRequest("::user_id::", GrantType.AUTHORIZATION_CODE, anyInstantTime, Maps.<String, String>newHashMap(), anyAuth.clientId)); will(returnValue(Optional.absent())); }}); Response response = identityAuthorizationActivity.execute(anyAuth, anyClient, request, anyInstantTime); assertThat(new RsPrint(response).printBody(), containsString("identity was not found")); }
@Test public void identityParamsArePassed() throws Exception { final Client anyClient = aNewClient().build(); final DateTime anyInstantTime = new DateTime(); final Authorization anyAuth = newAuthorization().addParam("index", "1").build(); context.checking(new Expectations() {{ oneOf(identityFinder).findIdentity(new FindIdentityRequest(anyAuth.identityId, GrantType.AUTHORIZATION_CODE, anyInstantTime, anyAuth.params, anyAuth.clientId)); will(returnValue(Optional.absent())); }}); identityAuthorizationActivity.execute(anyAuth, anyClient, aNewRequest().param("code", "::any code::").build(), anyInstantTime); }