Skip to content

Commit

Permalink
Require token-based authentication
Browse files Browse the repository at this point in the history
Closes gh-104
  • Loading branch information
wilkinsona committed Mar 14, 2024
1 parent 3e01a00 commit 40dff66
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
7 changes: 3 additions & 4 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ To generate a markdown changelog that comprise the list of bugs and issues in a
changelog:
repository: owner/name
github:
username:
password:
token:
----

NOTE: When generating a changelog for a public repository, the `username` and `password` properties are optional.
However, specifying them ensures that you don't hit https://developer.github.com/v3/?#rate-limiting[GitHub's rate limit] easily.
NOTE: When generating a changelog for a public repository, the `token` property is optional.
However, specifying it ensures that you don't hit https://developer.github.com/v3/?#rate-limiting[GitHub's rate limit] easily.

If you're using GitHub enterprise, the base url can be set using `github.api-url`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.util.StringUtils;

/**
* GitHub related properties.
Expand All @@ -35,32 +34,21 @@ public class GitHubProperties {
private final String apiUrl;

/**
* The username for the GitHub user.
* Token used for authentication.
*/
private final String username;
private final String token;

/**
* The password for the GitHub user.
*/
private final String password;

public GitHubProperties(@DefaultValue("https://api.github.com") String apiUrl, String username, String password,
String token) {
public GitHubProperties(@DefaultValue("https://api.github.com") String apiUrl, String token) {
this.apiUrl = apiUrl;
this.username = username;
this.password = StringUtils.hasText(token) ? token : password;
this.token = token;
}

public String getApiUrl() {
return this.apiUrl;
}

public String getUsername() {
return this.username;
}

public String getPassword() {
return this.password;
public String getToken() {
return this.token;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2022 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,10 +55,9 @@ public class GitHubService {
private final RestTemplate restTemplate;

public GitHubService(RestTemplateBuilder builder, GitHubProperties properties) {
String username = properties.getUsername();
String password = properties.getPassword();
if (StringUtils.hasLength(username)) {
builder = builder.basicAuthentication(username, password);
String token = properties.getToken();
if (StringUtils.hasLength(token)) {
builder = builder.defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
}
builder = builder.rootUri(properties.getApiUrl());
this.restTemplate = builder.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2023 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,8 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -35,6 +36,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
Expand All @@ -46,7 +48,6 @@
* @author Madhura Bhave
*/
@RestClientTest(GitHubService.class)
@MockBean(GitHubProperties.class)
class GitHubServiceTests {

private static final String MILESTONES_URL = "/repos/org/repo/milestones?state=all&sort=due_on&direction=desc&per_page=50";
Expand Down Expand Up @@ -115,7 +116,9 @@ void getIssuesWhenMultiplePagesOfIssuesPresent() {
}

private ResponseActions expectGet(String expectedUri) {
return this.server.expect(requestTo(expectedUri)).andExpect(method(HttpMethod.GET));
return this.server.expect(requestTo(expectedUri))
.andExpect(method(HttpMethod.GET))
.andExpect(header(HttpHeaders.AUTHORIZATION, "Bearer the-bearer-token"));
}

private DefaultResponseCreator withJsonFrom(String path) {
Expand All @@ -130,4 +133,14 @@ private ClassPathResource getClassPathResource(String path) {
return new ClassPathResource(path, getClass());
}

@TestConfiguration
static class GitHubPropertiesConfiguration {

@Bean
GitHubProperties gitHubProperties() {
return new GitHubProperties("https://api.github.com", "the-bearer-token");
}

}

}

0 comments on commit 40dff66

Please sign in to comment.