package cz.moneta.test.dsl.util.task.cagw.authorization;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.endpoints.cagw.CaGwEndpoint;

public class AuthorizationTasks {

    private final Harness harness;

    public AuthorizationTasks(Harness harness) {
        this.harness = harness;
    }
    public String getToken(String sourceAppName, String scope) {
        return getBearerToken("CID_" + sourceAppName, "CS_" + sourceAppName, scope);
    }

    public String getVoicebotToken() {
        return getToken("VOICEBOT", "voicebot");
    }

    private String getBearerToken(String key, String secret, String scope) {
        CaGwEndpoint endpoint = harness.getEndpoint(CaGwEndpoint.class);
        String username = endpoint.getClientSecret(key);
        String password = endpoint.getClientSecret(secret);

        return "Bearer " + harness.withCaGw()
                .auth().oauth().v2().token()
                .prepareOauth2V2TokenRequest()
                .client_id(username)
                .client_secret(password)
                .grant_type("client_credentials")
                .scope(scope)
                .send()
                .getAccess_token();
    }
}