Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PP-11867: Test getting the access token #1878

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/uk/gov/pay/api/app/config/PublicApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class PublicApiConfig extends Configuration {
@NotNull
@JsonProperty
private RedisConfiguration redis;

@JsonProperty
private TinkConfiguration tinkConfiguration;

@Valid
@NotNull
Expand Down Expand Up @@ -88,6 +91,10 @@ public RedisConfiguration getRedisConfiguration() {
return redis;
}

public TinkConfiguration getTinkConfiguration() {
return tinkConfiguration;
}

public Optional<URI> getEcsContainerMetadataUriV4() {
return Optional.ofNullable(ecsContainerMetadataUriV4);
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/uk/gov/pay/api/app/config/TinkConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package uk.gov.pay.api.app.config;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

public class TinkConfiguration {

@Valid
@NotNull
private String clientId;

@Valid
@NotNull
private String clientSecret;

public String getClientId() {
return clientId;
}

public String getClientSecret() {
return clientSecret;
}
}
34 changes: 34 additions & 0 deletions src/main/java/uk/gov/pay/api/model/tink/TinkTokenResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uk.gov.pay.api.model.tink;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

/*
for e.g.
{
"token_type" : "bearer",
"expires_in" : 1799,
"access_token" : "{YOUR_CLIENT_ACCESS_TOKEN}",
"scope" : "payment:read,payment:write",
"id_hint" : null
}
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class TinkTokenResponse {

private String tokenType;

private String accessToken;

public String getTokenType() {
return tokenType;
}

public String getAccessToken() {
return accessToken;
}
}
51 changes: 51 additions & 0 deletions src/main/java/uk/gov/pay/api/service/OpenBankingService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package uk.gov.pay.api.service;

import org.apache.http.HttpResponse;
import uk.gov.pay.api.app.config.TinkConfiguration;
import uk.gov.pay.api.model.tink.TinkTokenResponse;

import javax.inject.Inject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import static javax.ws.rs.core.Response.Status.OK;

public class OpenBankingService {

private final Client client;

private final TinkConfiguration tinkConfiguration;

@Inject
public OpenBankingService(Client client, TinkConfiguration tinkConfiguration) {
this.client = client;
this.tinkConfiguration = tinkConfiguration;
}


/*
Access token expire every 30 minutes so a to-do here would be to 1) refresh the token every 25 minutes on an
internal scheduler OR 2) refresh the token based on the expiry "expires_in"
*/
public String getAccessToken() {
MultivaluedMap<String, String> map = new MultivaluedHashMap<>(4);
map.add("client_id", tinkConfiguration.getClientId());
map.add("client_secret", tinkConfiguration.getClientSecret());
map.add("grant_type", "client_credentials");
map.add("scope", "payment:read,payment:write");
Response response = client.target("https://api.tink.com/api/v1/oauth/token")
.request()
.accept(MediaType.APPLICATION_JSON)
.post(Entity.form(map));

if (response.getStatus() == OK.getStatusCode()) {
return response.readEntity(TinkTokenResponse.class).getAccessToken();
} else {
throw new RuntimeException("Response from getting a Tink token was not 200.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uk.gov.pay.api.it.contract;

import io.dropwizard.testing.junit.DropwizardAppRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import uk.gov.pay.api.app.PublicApi;
import uk.gov.pay.api.app.config.PublicApiConfig;
import uk.gov.pay.api.service.OpenBankingService;

import javax.ws.rs.client.ClientBuilder;

import static io.dropwizard.testing.ResourceHelpers.resourceFilePath;

public class OpenBankingServiceTest {

@Rule
public DropwizardAppRule<PublicApiConfig> app = new DropwizardAppRule<>(
PublicApi.class,
resourceFilePath("config/test-config.yaml"));

private OpenBankingService openBankingService;

@Before
public void before() {
openBankingService = new OpenBankingService(ClientBuilder.newBuilder().build(),
app.getConfiguration().getTinkConfiguration());
}

@Test
public void testGetAccessToken() {
System.out.println(openBankingService.getAccessToken());
}
}
4 changes: 4 additions & 0 deletions src/test/resources/config/test-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ apiKeyHmacSecret: qwer9yuhgf
authenticationCachePolicy: expireAfterWrite=3s

ecsContainerMetadataUriV4: ${ECS_CONTAINER_METADATA_URI_V4:-}

tinkConfiguration:
clientId: ${TINK_CLIENT_ID:-}
clientSecret: ${TINK_CLIENT_SECRET:-}
Loading