Skip to content

Commit 929e677

Browse files
committed
added safe guards for when the oauth2 providers are null
1 parent 691c7a8 commit 929e677

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

server/src/main/java/dev/findfirst/users/controller/UserController.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import lombok.RequiredArgsConstructor;
3939
import lombok.extern.slf4j.Slf4j;
40+
import org.springframework.beans.factory.annotation.Autowired;
4041
import org.springframework.beans.factory.annotation.Value;
4142
import org.springframework.core.io.FileSystemResource;
4243
import org.springframework.core.io.Resource;
@@ -63,8 +64,12 @@ public class UserController {
6364

6465
private final RefreshTokenService refreshTokenService;
6566

66-
private final Oauth2SourceService oauth2SourceService;
67+
private Oauth2SourceService oauth2SourceService;
6768

69+
@Autowired(required = false)
70+
public void setOauth2SourceService(Oauth2SourceService oauth2SourceService) {
71+
this.oauth2SourceService = oauth2SourceService;
72+
}
6873

6974
@Value("${findfirst.app.frontend-url}")
7075
private String frontendUrl;
@@ -87,6 +92,10 @@ public ResponseEntity<User> userInfo() throws NoUserFoundException {
8792

8893
@GetMapping("/oauth2Providers")
8994
public ResponseEntity<List<Oauth2Source>> oauth2Providers() {
95+
if (oauth2SourceService == null) {
96+
// return a blank list.
97+
return ResponseEntity.ok(List.of());
98+
}
9099
return ResponseEntity.ok(oauth2SourceService.oauth2Sources());
91100
}
92101

server/src/main/java/dev/findfirst/users/service/Oauth2SourceService.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@ public class Oauth2SourceService {
2828

2929
@PostConstruct
3030
void init() {
31-
oauth2Providers.iterator().forEachRemaining(provider -> {
32-
var tknUri = provider.getProviderDetails().getTokenUri();
33-
log.debug("Token URI {}", tknUri);
34-
// skip http(s)://
35-
if (!tknUri.contains("https://")) {
36-
log.debug("provider without https {}", tknUri);
37-
// do we really want to trust anything that isn't https?
38-
return;
39-
}
40-
oauth2Sources.add(new Oauth2Source(provider.getClientName(), getFaviconURI(provider),
41-
"oauth2/authorization/" + provider.getRegistrationId()));
42-
});
31+
if (oauth2Providers != null) {
32+
oauth2Providers.iterator().forEachRemaining(provider -> {
33+
var tknUri = provider.getProviderDetails().getTokenUri();
34+
log.debug("Token URI {}", tknUri);
35+
// skip http(s)://
36+
if (!tknUri.contains("https://")) {
37+
log.debug("provider without https {}", tknUri);
38+
// do we really want to trust anything that isn't https?
39+
return;
40+
}
41+
oauth2Sources.add(new Oauth2Source(provider.getClientName(), getFaviconURI(provider),
42+
"oauth2/authorization/" + provider.getRegistrationId()));
43+
});
44+
}
4345
}
4446

4547
public List<Oauth2Source> oauth2Sources() {

server/src/test/java/dev/findfirst/users/controller/UserControllerTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dev.findfirst.users.controller;
22

33
import static dev.findfirst.utilities.HttpUtility.getHttpEntity;
4-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
54
import static org.junit.jupiter.api.Assertions.assertEquals;
65
import static org.junit.jupiter.api.Assertions.assertNotNull;
76
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -247,9 +246,8 @@ void testRemoveUserPhoto_Success() throws Exception {
247246
@Test
248247
void getAllProivders() {
249248
var response = restTemplate.getForEntity("/user/oauth2Providers", Oauth2Source[].class);
250-
251-
assertArrayEquals(new Oauth2Source[] {new Oauth2Source("GitHub",
252-
"https://github.com/favicon.ico", "oauth2/authorization/github")}, response.getBody());
253-
249+
var sources = response.getBody();
250+
assertTrue(sources.length == 1);
251+
assertEquals("GitHub", sources[0].provider(), "Github should be the provider.");
254252
}
255253
}

0 commit comments

Comments
 (0)