Skip to content

Commit 884e70e

Browse files
committed
Oauth2 Providers list
1 parent e8710d7 commit 884e70e

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import dev.findfirst.users.exceptions.NoUserFoundException;
2828
import dev.findfirst.users.exceptions.TokenExpiredException;
2929
import dev.findfirst.users.exceptions.UserNameTakenException;
30+
import dev.findfirst.users.model.oauth2.Oauth2Source;
3031
import dev.findfirst.users.model.user.SigninTokens;
3132
import dev.findfirst.users.model.user.TokenPassword;
3233
import dev.findfirst.users.model.user.User;
@@ -87,14 +88,28 @@ public ResponseEntity<User> userInfo() throws NoUserFoundException {
8788
}
8889

8990
@GetMapping("/oauth2Providers")
90-
public ResponseEntity<List<String>> oauth2Providers() {
91-
List<String> listOfAuth2Providers = new ArrayList<>();
91+
public ResponseEntity<List<Oauth2Source>> oauth2Providers() {
92+
List<Oauth2Source> listOfAuth2Providers = new ArrayList<>();
9293
if (oauth2Providers == null) {
9394
return ResponseEntity.ofNullable(listOfAuth2Providers);
9495
}
9596
oauth2Providers.iterator().forEachRemaining(provider -> {
96-
log.debug(provider.getProviderDetails().getTokenUri());
97-
listOfAuth2Providers.add(provider.getRegistrationId());
97+
var tknUri = provider.getProviderDetails().getTokenUri();
98+
log.debug("Token URI {}", tknUri);
99+
// skip http(s)://
100+
var noProto = "";
101+
if (tknUri.contains("https://")) {
102+
noProto = tknUri.substring(8);
103+
} else {
104+
log.debug("provider without https {}", tknUri);
105+
// do we really want to trust anything that isn't https?
106+
return;
107+
}
108+
var domain = noProto.indexOf("/");
109+
var faviconURI = "https://" + noProto.substring(0, domain) + "/favicon.ico";
110+
111+
log.debug("Favicon URI {}", faviconURI);
112+
listOfAuth2Providers.add(new Oauth2Source(provider.getClientName(), faviconURI));
98113
});
99114
return ResponseEntity.ofNullable(listOfAuth2Providers);
100115
}
@@ -200,8 +215,7 @@ public ResponseEntity<String> refreshToken(
200215
public ResponseEntity<String> uploadProfilePicture(
201216
@Valid @RequestParam("file") @FileSize MultipartFile file) throws NoUserFoundException {
202217
log.debug("Attempting to add user profile picture");
203-
User user =
204-
userService.getUserById(uContext.getUserId()).orElseThrow(NoUserFoundException::new);
218+
User user = userService.getUserById(uContext.getUserId()).orElseThrow(NoUserFoundException::new);
205219

206220
// File type validation
207221
String contentType = file.getContentType();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package dev.findfirst.users.model.oauth2;
2+
3+
public record Oauth2Source(String provider, String iconUrl) {
4+
5+
}

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import dev.findfirst.security.userauth.models.TokenRefreshResponse;
1818
import dev.findfirst.security.userauth.models.payload.request.SignupRequest;
1919
import dev.findfirst.users.model.MailHogMessage;
20+
import dev.findfirst.users.model.oauth2.Oauth2Source;
2021
import dev.findfirst.users.model.user.TokenPassword;
2122

2223
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -49,9 +50,9 @@
4950
@IntegrationTest
5051
@MockTypesense
5152
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
52-
@TestPropertySource(locations = "classpath:application-test.yml",
53-
properties = {"spring.security.oauth2.client.registration.github.client-secret=secret-oauth",
54-
"spring.security.oauth2.client.registration.github.client-id=test-id"})
53+
@TestPropertySource(locations = "classpath:application-test.yml", properties = {
54+
"spring.security.oauth2.client.registration.github.client-secret=secret-oauth",
55+
"spring.security.oauth2.client.registration.github.client-id=test-id" })
5556
class UserControllerTest {
5657

5758
TestRestTemplate restTemplate = new TestRestTemplate();
@@ -72,8 +73,8 @@ class UserControllerTest {
7273
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16.2-alpine3.19");
7374

7475
@Container
75-
public static GenericContainer<?> mailhog =
76-
new GenericContainer<>(DockerImageName.parse("mailhog/mailhog:latest")).withExposedPorts(1025,
76+
public static GenericContainer<?> mailhog = new GenericContainer<>(DockerImageName.parse("mailhog/mailhog:latest"))
77+
.withExposedPorts(1025,
7778
8025);
7879

7980
@TestConfiguration
@@ -99,7 +100,8 @@ public JavaMailSender javaMailSender() {
99100
private String userUrl = "/user";
100101

101102
/**
102-
* Tests that a user should be able to sign up. After signing up another user should not be able
103+
* Tests that a user should be able to sign up. After signing up another user
104+
* should not be able
103105
* use the same username or email.
104106
*/
105107
@Test
@@ -116,7 +118,8 @@ void userSignup() {
116118
}
117119

118120
/**
119-
* Create a user, gets the registration token from the email. Uses the token to complete
121+
* Create a user, gets the registration token from the email. Uses the token to
122+
* complete
120123
* registration.
121124
*/
122125
@Test
@@ -245,9 +248,10 @@ void testRemoveUserPhoto_Success() throws Exception {
245248

246249
@Test
247250
void getAllProivders() {
248-
var response = restTemplate.getForEntity("/user/oauth2Providers", String[].class);
251+
var response = restTemplate.getForEntity("/user/oauth2Providers", Oauth2Source[].class);
249252

250-
assertArrayEquals(new String[] {"github"}, response.getBody());
253+
assertArrayEquals(new Oauth2Source[] { new Oauth2Source("GitHub", "https://github.com/favicon.ico") },
254+
response.getBody());
251255

252256
}
253257
}

0 commit comments

Comments
 (0)