-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
201 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,23 +34,23 @@ private void init() { | |
Member member1 = memberRepository.save(new Member( | ||
"[email protected]", | ||
OAuth2Provider.GITHUB, | ||
111111L, | ||
"111111", | ||
"Alice", | ||
"https://cdn-icons-png.flaticon.com/128/4472/4472552.png" | ||
)); | ||
|
||
Member member2 = memberRepository.save(new Member( | ||
"[email protected]", | ||
OAuth2Provider.GITHUB, | ||
222222L, | ||
"222222", | ||
"Bob", | ||
"https://cdn-icons-png.flaticon.com/128/4472/4472525.png" | ||
)); | ||
|
||
Member member3 = memberRepository.save(new Member( | ||
"[email protected]", | ||
OAuth2Provider.GITHUB, | ||
333333L, | ||
"333333", | ||
"Charlie", | ||
"https://cdn-icons-png.flaticon.com/128/4472/4472516.png" | ||
)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
public enum OAuth2Provider { | ||
|
||
GITHUB, | ||
GOOGLE, | ||
; | ||
|
||
public static OAuth2Provider from(String provider) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
server/src/main/java/com/fluffy/oauth2/infra/google/GoogleOAuth2Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.fluffy.oauth2.infra.google; | ||
|
||
import static org.springframework.http.HttpHeaders.AUTHORIZATION; | ||
import static org.springframework.http.MediaType.APPLICATION_JSON; | ||
|
||
import com.fluffy.oauth2.infra.google.dto.GoogleAccessTokenRequest; | ||
import com.fluffy.oauth2.infra.google.dto.GoogleAccessTokenResponse; | ||
import com.fluffy.oauth2.infra.google.dto.GoogleUserInfoResponse; | ||
import java.time.Duration; | ||
import org.springframework.boot.web.client.ClientHttpRequestFactories; | ||
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings; | ||
import org.springframework.http.client.ClientHttpRequestFactory; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@Component | ||
public class GoogleOAuth2Client { | ||
|
||
private final GoogleOAuth2Properties properties; | ||
private final RestClient restClient; | ||
|
||
public GoogleOAuth2Client(GoogleOAuth2Properties properties, RestClient.Builder restClientBuilder) { | ||
this.properties = properties; | ||
this.restClient = createRestClient(restClientBuilder); | ||
} | ||
|
||
private RestClient createRestClient(RestClient.Builder restClientBuilder) { | ||
return restClientBuilder.requestFactory(clientHttpRequestFactory()).build(); | ||
} | ||
|
||
private ClientHttpRequestFactory clientHttpRequestFactory() { | ||
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.DEFAULTS | ||
.withConnectTimeout(Duration.ofSeconds(3)) | ||
.withReadTimeout(Duration.ofSeconds(5)); | ||
|
||
return ClientHttpRequestFactories.get(settings); | ||
} | ||
|
||
public GoogleAccessTokenResponse fetchAccessToken(String code) { | ||
GoogleAccessTokenRequest request = new GoogleAccessTokenRequest( | ||
code, | ||
properties.clientId(), | ||
properties.clientSecret(), | ||
"authorization_code", | ||
properties.redirectUri() | ||
); | ||
|
||
return restClient.post() | ||
.uri("https://oauth2.googleapis.com/token") | ||
.contentType(APPLICATION_JSON) | ||
.accept(APPLICATION_JSON) | ||
.body(request) | ||
.retrieve() | ||
.body(GoogleAccessTokenResponse.class); | ||
} | ||
|
||
public GoogleUserInfoResponse fetchUserInfo(String accessToken) { | ||
return restClient.get() | ||
.uri("https://www.googleapis.com/oauth2/v2/userinfo") | ||
.header(AUTHORIZATION, String.format("Bearer %s", accessToken)) | ||
.accept(APPLICATION_JSON) | ||
.retrieve() | ||
.body(GoogleUserInfoResponse.class); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
server/src/main/java/com/fluffy/oauth2/infra/google/GoogleOAuth2Properties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.fluffy.oauth2.infra.google; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties("auth.oauth2.google") | ||
public record GoogleOAuth2Properties( | ||
@NotBlank String clientId, | ||
@NotBlank String clientSecret, | ||
@NotBlank String redirectUri, | ||
@NotBlank String clientUri | ||
) { | ||
} |
52 changes: 52 additions & 0 deletions
52
server/src/main/java/com/fluffy/oauth2/infra/google/GoogleOAuth2Strategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.fluffy.oauth2.infra.google; | ||
|
||
import com.fluffy.auth.domain.OAuth2Provider; | ||
import com.fluffy.oauth2.application.OAuth2Strategy; | ||
import com.fluffy.oauth2.domain.OAuth2UserInfo; | ||
import com.fluffy.oauth2.infra.google.dto.GoogleAccessTokenResponse; | ||
import com.fluffy.oauth2.infra.google.dto.GoogleUserInfoResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class GoogleOAuth2Strategy implements OAuth2Strategy { | ||
|
||
private final GoogleOAuth2Client googleOAuth2Client; | ||
private final GoogleOAuth2Properties properties; | ||
|
||
@Override | ||
public String buildOAuth2LoginUrl(String next) { | ||
return UriComponentsBuilder.fromHttpUrl("https://accounts.google.com/o/oauth2/v2/auth") | ||
.queryParam("client_id", properties.clientId()) | ||
.queryParam("redirect_uri", properties.redirectUri()) | ||
.queryParam("response_type", "code") | ||
.queryParam("scope", "email profile") | ||
.build() | ||
.toUriString(); | ||
} | ||
|
||
@Override | ||
public OAuth2UserInfo fetchOAuth2UserInfo(String code) { | ||
GoogleAccessTokenResponse accessTokenResponse = googleOAuth2Client.fetchAccessToken(code); | ||
String accessToken = accessTokenResponse.accessToken(); | ||
|
||
GoogleUserInfoResponse userInfoResponse = googleOAuth2Client.fetchUserInfo(accessToken); | ||
|
||
return userInfoResponse.toOAuth2UserInfo(); | ||
} | ||
|
||
@Override | ||
public String buildClientRedirectUrl(String next) { | ||
return UriComponentsBuilder.fromHttpUrl(properties.clientUri()) | ||
.path(next) | ||
.build() | ||
.toUriString(); | ||
} | ||
|
||
@Override | ||
public OAuth2Provider getProvider() { | ||
return OAuth2Provider.GOOGLE; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
server/src/main/java/com/fluffy/oauth2/infra/google/dto/GoogleAccessTokenRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.fluffy.oauth2.infra.google.dto; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
|
||
@JsonNaming(SnakeCaseStrategy.class) | ||
public record GoogleAccessTokenRequest( | ||
String code, | ||
String clientId, | ||
String clientSecret, | ||
String grantType, | ||
String redirectUri | ||
) { | ||
} |
8 changes: 8 additions & 0 deletions
8
server/src/main/java/com/fluffy/oauth2/infra/google/dto/GoogleAccessTokenResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.fluffy.oauth2.infra.google.dto; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
|
||
@JsonNaming(SnakeCaseStrategy.class) | ||
public record GoogleAccessTokenResponse(String accessToken) { | ||
} |
23 changes: 23 additions & 0 deletions
23
server/src/main/java/com/fluffy/oauth2/infra/google/dto/GoogleUserInfoResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.fluffy.oauth2.infra.google.dto; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import com.fluffy.oauth2.domain.OAuth2UserInfo; | ||
|
||
@JsonNaming(SnakeCaseStrategy.class) | ||
public record GoogleUserInfoResponse( | ||
String id, | ||
String email, | ||
String name, | ||
String picture | ||
) { | ||
|
||
public OAuth2UserInfo toOAuth2UserInfo() { | ||
return new OAuth2UserInfo( | ||
id, | ||
name, | ||
email, | ||
picture | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ public static MemberBuilder defaultMember() { | |
.withId(null) | ||
.withEmail("[email protected]") | ||
.withProvider(OAuth2Provider.GITHUB) | ||
.withSocialId(1234567890L) | ||
.withSocialId("1234567890") | ||
.withName("example") | ||
.withAvatarUrl("https://example.com/image.jpg"); | ||
} | ||
|
@@ -20,7 +20,7 @@ public static class MemberBuilder { | |
private Long id; | ||
private String email; | ||
private OAuth2Provider provider; | ||
private Long socialId; | ||
private String socialId; | ||
private String name; | ||
private String avatarUrl; | ||
|
||
|
@@ -39,7 +39,7 @@ public MemberBuilder withProvider(OAuth2Provider provider) { | |
return this; | ||
} | ||
|
||
public MemberBuilder withSocialId(Long socialId) { | ||
public MemberBuilder withSocialId(String socialId) { | ||
this.socialId = socialId; | ||
return this; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters