Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.rules.SecurityRule;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.breedinginsight.api.auth.AuthenticatedUser;
import org.breedinginsight.api.auth.SecurityService;
import org.breedinginsight.model.ApiToken;
import org.breedinginsight.services.TokenService;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.validation.constraints.NotBlank;
import java.net.URI;
import java.util.Map;
import java.util.Optional;

@Slf4j
Expand All @@ -47,28 +50,34 @@ public TokenController(SecurityService securityService, TokenService tokenServic
this.tokenService = tokenService;
}

@Get("/api-token")
@Get("/api-token{?returnUrl}")
@Secured(SecurityRule.IS_AUTHENTICATED)
public HttpResponse apiToken(@QueryValue @NotBlank String returnUrl) {
public HttpResponse apiToken(@QueryValue @Nullable String returnUrl) {

AuthenticatedUser actingUser = securityService.getUser();
Optional<ApiToken> token = tokenService.generateApiToken(actingUser);

if(token.isPresent()) {
ApiToken apiToken = token.get();

URI location = UriBuilder.of(returnUrl)
.queryParam("status", 200)
.queryParam("token", apiToken.getAccessToken())
.build();
if(returnUrl != null) {
if(StringUtils.trim(returnUrl).isEmpty()) {
return HttpResponse.badRequest("returnUrl cannot be blank");
}
URI location = UriBuilder.of(returnUrl)
.queryParam("status", 200)
.queryParam("token", apiToken.getAccessToken())
.build();

return HttpResponse.seeOther(location)
.header("Cache-Control","no-store")
.header("Pragma", "no-cache");
return HttpResponse.seeOther(location)
.header("Cache-Control", "no-store")
.header("Pragma", "no-cache");
} else {
return HttpResponse.ok(Map.of("token", apiToken.getAccessToken()));
}
} else {
return HttpResponse.serverError();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ public BrAPIV2Controller(SecurityService securityService, ProgramService program
@Secured(SecurityRule.IS_ANONYMOUS)
public BrAPIServerInfoResponse serverinfo() {
BrAPIServerInfo serverInfo = new BrAPIServerInfo();
serverInfo.setOrganizationName("Breeding Insight Platform");
serverInfo.setServerName("bi-api");
serverInfo.setOrganizationName("Breeding Insight");
serverInfo.setServerName("DeltaBreed");
serverInfo.setContactEmail("bidevteam@cornell.edu");
serverInfo.setOrganizationURL("breedinginsight.org");
serverInfo.setServerDescription("BrAPI endpoints are not implemented at the root of this domain. Please make BrAPI calls in the context of a program (ex: https://app.breedinginsight.net/v1/programs/<programId>/brapi/v2)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


return new BrAPIServerInfoResponse().result(serverInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ public class TokenControllerIntegrationTest extends DatabaseTest {
@Client("/${micronaut.bi.api.version}")
RxHttpClient client;

@Test
void getApiTokenMissingRequiredParameter() {
Flowable<HttpResponse<String>> call = client.exchange(
GET("/api-token")
.cookie(new NettyCookie("phylo-token", "test-registered-user")), String.class
);

HttpClientResponseException e = Assertions.assertThrows(HttpClientResponseException.class, () -> {
HttpResponse<String> response = call.blockingFirst();
});
assertEquals(HttpStatus.BAD_REQUEST, e.getStatus());
}

@Test
void getApiTokenRequiredParameterBlank() {
Flowable<HttpResponse<String>> call = client.exchange(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ public void testRootServerInfo() {
.getAsJsonObject("result");
BrAPIServerInfo serverInfo = GSON.fromJson(result, BrAPIServerInfo.class);

assertEquals("Breeding Insight Platform", serverInfo.getOrganizationName());
assertEquals("bi-api", serverInfo.getServerName());
assertEquals("Breeding Insight", serverInfo.getOrganizationName());
assertEquals("DeltaBreed", serverInfo.getServerName());
assertEquals("bidevteam@cornell.edu", serverInfo.getContactEmail());
assertEquals("breedinginsight.org", serverInfo.getOrganizationURL());
}
Expand Down