// Path: src/main/java/org/osiam/auth/oauth_client/OsiamAuthServerClientProvider.java
// @Service
// public class OsiamAuthServerClientProvider {
// 
//     private static final Logger LOGGER = LoggerFactory.getLogger(OsiamAuthServerClientProvider.class.getName());
// 
//     public static final String AUTH_SERVER_CLIENT_ID = "auth-server";
//     public static final int CLIENT_VALIDITY = 10;
// 
//     @Autowired
//     private PlatformTransactionManager txManager;
// 
//     @Autowired
//     private ClientRepository clientRepository;
// 
//     @Value("${org.osiam.auth-server.home}")
//     private String authServerHome;
// 
//     private String authServerClientSecret;
// 
//     @PostConstruct
//     private void createAuthServerClient() {
//         TransactionTemplate transactionTemplate = new TransactionTemplate(txManager);
//         transactionTemplate.execute(new TransactionCallbackWithoutResult() {
//             @Override
//             protected void doInTransactionWithoutResult(TransactionStatus status) {
//                 if (!clientRepository.existsById(AUTH_SERVER_CLIENT_ID)) {
//                     LOGGER.info("No auth server client found, so it will be created.");
//                     int validity = CLIENT_VALIDITY;
// 
//                     ClientEntity clientEntity = new ClientEntity();
//                     Set<String> scopes = new HashSet<>();
//                     scopes.add(Scope.ADMIN.toString());
// 
//                     Set<String> grants = new HashSet<>();
//                     grants.add(GrantType.CLIENT_CREDENTIALS.toString());
// 
//                     clientEntity.setClientId(AUTH_SERVER_CLIENT_ID);
//                     clientEntity.setRefreshTokenValiditySeconds(validity);
//                     clientEntity.setAccessTokenValiditySeconds(validity);
//                     clientEntity.setRedirectUri(authServerHome);
//                     clientEntity.setScope(scopes);
//                     clientEntity.setImplicit(true);
//                     clientEntity.setValidityInSeconds(validity);
//                     clientEntity.setGrants(grants);
// 
//                     clientEntity = clientRepository.save(clientEntity);
//                     authServerClientSecret = clientEntity.getClientSecret();
//                 }
//             }
//         });
//     }
// 
//     public String getClientSecret() {
//         return authServerClientSecret;
//     }
// }


import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.osiam.auth.oauth_client.OsiamAuthServerClientProvider;
import org.osiam.client.oauth.AccessToken;
import org.osiam.client.oauth.Scope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
/*
 * Copyright (C) 2013 tarent AG
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package org.osiam.auth.token;



/**
 * TokenProvider which created an accesstoken for the auth server client
 */
@Service
public class OsiamAccessTokenProvider {

    @Autowired
    private TokenStore tokenStore;

    public AccessToken createAccessToken() {
        Set<String> scopes = new HashSet<>();
        scopes.add(Scope.ADMIN.toString());
        // Random scope, because the token services generates for every scope but same client
        // a different access token. This is only made due to the token expired problem, when the auth server
        // takes his actual access token, but the token is expired during the request to the resource server
        scopes.add(new Scope(UUID.randomUUID().toString()).toString());

        Map<String, String> parameters = new HashMap<>();
// Path: src/main/java/org/osiam/auth/oauth_client/OsiamAuthServerClientProvider.java
// @Service
// public class OsiamAuthServerClientProvider {
// 
//     private static final Logger LOGGER = LoggerFactory.getLogger(OsiamAuthServerClientProvider.class.getName());
// 
//     public static final String AUTH_SERVER_CLIENT_ID = "auth-server";
//     public static final int CLIENT_VALIDITY = 10;
// 
//     @Autowired
//     private PlatformTransactionManager txManager;
// 
//     @Autowired
//     private ClientRepository clientRepository;
// 
//     @Value("${org.osiam.auth-server.home}")
//     private String authServerHome;
// 
//     private String authServerClientSecret;
// 
//     @PostConstruct
//     private void createAuthServerClient() {
//         TransactionTemplate transactionTemplate = new TransactionTemplate(txManager);
//         transactionTemplate.execute(new TransactionCallbackWithoutResult() {
//             @Override
//             protected void doInTransactionWithoutResult(TransactionStatus status) {
//                 if (!clientRepository.existsById(AUTH_SERVER_CLIENT_ID)) {
//                     LOGGER.info("No auth server client found, so it will be created.");
//                     int validity = CLIENT_VALIDITY;
// 
//                     ClientEntity clientEntity = new ClientEntity();
//                     Set<String> scopes = new HashSet<>();
//                     scopes.add(Scope.ADMIN.toString());
// 
//                     Set<String> grants = new HashSet<>();
//                     grants.add(GrantType.CLIENT_CREDENTIALS.toString());
// 
//                     clientEntity.setClientId(AUTH_SERVER_CLIENT_ID);
//                     clientEntity.setRefreshTokenValiditySeconds(validity);
//                     clientEntity.setAccessTokenValiditySeconds(validity);
//                     clientEntity.setRedirectUri(authServerHome);
//                     clientEntity.setScope(scopes);
//                     clientEntity.setImplicit(true);
//                     clientEntity.setValidityInSeconds(validity);
//                     clientEntity.setGrants(grants);
// 
//                     clientEntity = clientRepository.save(clientEntity);
//                     authServerClientSecret = clientEntity.getClientSecret();
//                 }
//             }
//         });
//     }
// 
//     public String getClientSecret() {
//         return authServerClientSecret;
//     }
// }


// Path: src/main/java/org/osiam/auth/token/OsiamAccessTokenProvider.java
import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.osiam.auth.oauth_client.OsiamAuthServerClientProvider;
import org.osiam.client.oauth.AccessToken;
import org.osiam.client.oauth.Scope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

/*
 * Copyright (C) 2013 tarent AG
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package org.osiam.auth.token;



/**
 * TokenProvider which created an accesstoken for the auth server client
 */
@Service
public class OsiamAccessTokenProvider {

    @Autowired
    private TokenStore tokenStore;

    public AccessToken createAccessToken() {
        Set<String> scopes = new HashSet<>();
        scopes.add(Scope.ADMIN.toString());
        // Random scope, because the token services generates for every scope but same client
        // a different access token. This is only made due to the token expired problem, when the auth server
        // takes his actual access token, but the token is expired during the request to the resource server
        scopes.add(new Scope(UUID.randomUUID().toString()).toString());

        Map<String, String> parameters = new HashMap<>();
        parameters.put("client_id", OsiamAuthServerClientProvider.AUTH_SERVER_CLIENT_ID);