All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.adorsys.ledgers.keycloak.client.mapper.KeycloakAuthMapperImpl Maven / Gradle / Ivy

The newest version!
package de.adorsys.ledgers.keycloak.client.mapper;

import de.adorsys.ledgers.middleware.api.domain.um.AccessTokenTO;
import de.adorsys.ledgers.middleware.api.domain.um.BearerTokenTO;
import javax.annotation.processing.Generated;
import org.keycloak.adapters.RefreshableKeycloakSecurityContext;
import org.keycloak.representations.AccessToken;
import org.keycloak.representations.AccessTokenResponse;
import org.springframework.stereotype.Component;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-12-06T13:24:18+0000",
    comments = "version: 1.4.1.Final, compiler: javac, environment: Java 11.0.13 (Azul Systems, Inc.)"
)
@Component
public class KeycloakAuthMapperImpl implements KeycloakAuthMapper {

    @Override
    public AccessTokenTO toAccessToken(RefreshableKeycloakSecurityContext source) {
        if ( source == null ) {
            return null;
        }

        AccessTokenTO accessTokenTO = new AccessTokenTO();

        Integer issuedAt = sourceTokenIssuedAt( source );
        if ( issuedAt != null ) {
            accessTokenTO.setIat( toDate( issuedAt.longValue() ) );
        }
        accessTokenTO.setSub( sourceTokenSubject( source ) );
        String scope = sourceTokenScope( source );
        accessTokenTO.setScopes( toScopes( scope ) );
        accessTokenTO.setLogin( sourceTokenName( source ) );
        Long exp = sourceTokenExp( source );
        if ( exp != null ) {
            accessTokenTO.setExp( toDate( exp.longValue() ) );
        }
        accessTokenTO.setJti( sourceTokenId( source ) );
        accessTokenTO.setAccessToken( source.getTokenString() );

        accessTokenTO.setRole( getLedgersUserRoles(source.getToken()) );
        accessTokenTO.setTokenUsage( de.adorsys.ledgers.middleware.api.domain.um.TokenUsageTO.DIRECT_ACCESS );

        return accessTokenTO;
    }

    @Override
    public BearerTokenTO toBearerTokenTO(AccessTokenResponse source) {
        if ( source == null ) {
            return null;
        }

        BearerTokenTO bearerTokenTO = new BearerTokenTO();

        bearerTokenTO.setScopes( toScopes( source.getScope() ) );
        bearerTokenTO.setAccess_token( source.getToken() );
        bearerTokenTO.setExpires_in( (int) source.getExpiresIn() );
        bearerTokenTO.setRefresh_token( source.getRefreshToken() );
        bearerTokenTO.setToken_type( source.getTokenType() );

        return bearerTokenTO;
    }

    @Override
    public AccessTokenTO toTokenTO(AccessToken source, String token) {
        if ( source == null && token == null ) {
            return null;
        }

        AccessTokenTO accessTokenTO = new AccessTokenTO();

        if ( source != null ) {
            if ( source.getExp() != null ) {
                accessTokenTO.setExp( toDate( source.getExp().longValue() ) );
            }
            accessTokenTO.setJti( source.getId() );
            accessTokenTO.setSub( source.getSubject() );
            if ( source.getIat() != null ) {
                accessTokenTO.setIat( toDate( source.getIat().longValue() ) );
            }
            accessTokenTO.setScopes( toScopes( source.getScope() ) );
            accessTokenTO.setRole( getLedgersUserRoles( source ) );
            accessTokenTO.setLogin( source.getName() );
        }
        if ( token != null ) {
            accessTokenTO.setAccessToken( token );
        }
        accessTokenTO.setTokenUsage( de.adorsys.ledgers.middleware.api.domain.um.TokenUsageTO.DIRECT_ACCESS );

        return accessTokenTO;
    }

    private Integer sourceTokenIssuedAt(RefreshableKeycloakSecurityContext refreshableKeycloakSecurityContext) {
        if ( refreshableKeycloakSecurityContext == null ) {
            return null;
        }
        AccessToken token = refreshableKeycloakSecurityContext.getToken();
        if ( token == null ) {
            return null;
        }
        int issuedAt = token.getIssuedAt();
        return issuedAt;
    }

    private String sourceTokenSubject(RefreshableKeycloakSecurityContext refreshableKeycloakSecurityContext) {
        if ( refreshableKeycloakSecurityContext == null ) {
            return null;
        }
        AccessToken token = refreshableKeycloakSecurityContext.getToken();
        if ( token == null ) {
            return null;
        }
        String subject = token.getSubject();
        if ( subject == null ) {
            return null;
        }
        return subject;
    }

    private String sourceTokenScope(RefreshableKeycloakSecurityContext refreshableKeycloakSecurityContext) {
        if ( refreshableKeycloakSecurityContext == null ) {
            return null;
        }
        AccessToken token = refreshableKeycloakSecurityContext.getToken();
        if ( token == null ) {
            return null;
        }
        String scope = token.getScope();
        if ( scope == null ) {
            return null;
        }
        return scope;
    }

    private String sourceTokenName(RefreshableKeycloakSecurityContext refreshableKeycloakSecurityContext) {
        if ( refreshableKeycloakSecurityContext == null ) {
            return null;
        }
        AccessToken token = refreshableKeycloakSecurityContext.getToken();
        if ( token == null ) {
            return null;
        }
        String name = token.getName();
        if ( name == null ) {
            return null;
        }
        return name;
    }

    private Long sourceTokenExp(RefreshableKeycloakSecurityContext refreshableKeycloakSecurityContext) {
        if ( refreshableKeycloakSecurityContext == null ) {
            return null;
        }
        AccessToken token = refreshableKeycloakSecurityContext.getToken();
        if ( token == null ) {
            return null;
        }
        Long exp = token.getExp();
        if ( exp == null ) {
            return null;
        }
        return exp;
    }

    private String sourceTokenId(RefreshableKeycloakSecurityContext refreshableKeycloakSecurityContext) {
        if ( refreshableKeycloakSecurityContext == null ) {
            return null;
        }
        AccessToken token = refreshableKeycloakSecurityContext.getToken();
        if ( token == null ) {
            return null;
        }
        String id = token.getId();
        if ( id == null ) {
            return null;
        }
        return id;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy