class ZosmfAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) {
        final String user = authentication.getPrincipal().toString();

        final ZosmfService.AuthenticationResponse ar = zosmfService.authenticate(authentication);

        // if z/OSMF support JWT, use it as Zowe JWT token
        if (ar.getTokens().containsKey(JWT) && useJwtToken) {
            return authenticationService.createTokenAuthentication(user, ar.getTokens().get(JWT));
        }

        if (ar.getTokens().containsKey(LTPA)) {
            // construct own JWT token, including LTPA from z/OSMF
            final String domain = ar.getDomain();
            final String jwtToken = authenticationService.createJwtToken(user, domain, ar.getTokens().get(LTPA));

            return authenticationService.createTokenAuthentication(user, jwtToken);
        }

        // JWT and LTPA tokens are missing, authentication was wrong
        throw new BadCredentialsException("Username or password are invalid.");
    }

}