Java Code Examples for org.keycloak.representations.AccessToken#setPreferredUsername()

The following examples show how to use org.keycloak.representations.AccessToken#setPreferredUsername() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IdentityServiceRemoteUserMapperTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Utility method to create tokens for testing.
 * 
 * @param expired Determines whether to create an expired JWT
 * @return The string representation of the JWT
 */
private String generateToken(boolean expired) throws Exception
{
    String issuerUrl = this.identityServiceConfig.getAuthServerUrl() + "/realms/" + this.identityServiceConfig.getRealm();
    
    AccessToken token = new AccessToken();
    token.type("Bearer");
    token.id("1234");
    token.subject("abc123");
    token.issuer(issuerUrl);
    token.setPreferredUsername(TEST_USER_USERNAME);
    token.setEmail(TEST_USER_EMAIL);
    token.setGivenName("Joe");
    token.setFamilyName("Bloggs");
    
    if (expired)
    {
        token.expiration(Time.currentTime() - 60);
    }

    String jwt = new JWSBuilder()
            .jsonContent(token)
            .rsa256(keyPair.getPrivate());
    
    return jwt;
}
 
Example 2
Source File: AbstractBaseServiceTest.java    From aerogear-unifiedpush-server with Apache License 2.0 6 votes vote down vote up
/**
 * Basic setup stuff, needed for all the UPS related service classes
 */
@Before
public void setUp() throws SystemException, NotSupportedException {
    // Keycloak test environment
    AccessToken token = new AccessToken();
    MockitoAnnotations.initMocks(this);
    //The current developer will always be the admin in this testing scenario
    token.setPreferredUsername("admin");
    when(context.getToken()).thenReturn(token);
    when(keycloakPrincipal.getKeycloakSecurityContext()).thenReturn(context);
    when(httpServletRequest.getUserPrincipal()).thenReturn(keycloakPrincipal);

    // glue it to serach mgr
    searchManager.setHttpServletRequest(httpServletRequest);

    // more to setup ?
    specificSetup();
}
 
Example 3
Source File: ClaimInformationPointProviderTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private HttpFacade createHttpFacade(Map<String, List<String>> headers, InputStream requestBody) {
    return new OIDCHttpFacade() {
        private Request request;

        @Override
        public KeycloakSecurityContext getSecurityContext() {
            AccessToken token = new AccessToken();

            token.subject("sub");
            token.setPreferredUsername("username");
            token.getOtherClaims().put("custom_claim", Arrays.asList("param-other-claims-value1", "param-other-claims-value2"));

            IDToken idToken = new IDToken();

            idToken.subject("sub");
            idToken.setPreferredUsername("username");
            idToken.getOtherClaims().put("custom_claim", Arrays.asList("param-other-claims-value1", "param-other-claims-value2"));

            return new KeycloakSecurityContext("tokenString", token, "idTokenString", idToken);
        }

        @Override
        public Request getRequest() {
            if (request == null) {
                request = createHttpRequest(headers, requestBody);
            }
            return request;
        }

        @Override
        public Response getResponse() {
            return createHttpResponse();
        }

        @Override
        public X509Certificate[] getCertificateChain() {
            return new X509Certificate[0];
        }
    };
}