This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BE] feat: Notion OAuth 연동 기능 구현 (#244)
* feat: notion oauth 연동 기능 구현 * refactor: `ConnectionController`로 통합 및 환경 변수 추가 * chore: 환경 변수 설정 변경 * style: 컨벤션 적용 * refactor: static 제거
- Loading branch information
cheon-eunjeong
authored
Aug 11, 2023
1 parent
fc53c73
commit ab9a25b
Showing
15 changed files
with
133 additions
and
26 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
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
59 changes: 59 additions & 0 deletions
59
...rc/main/java/org/donggle/backend/application/service/oauth/notion/NotionOAuthService.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,59 @@ | ||
package org.donggle.backend.application.service.oauth.notion; | ||
|
||
import org.donggle.backend.application.service.oauth.notion.dto.NotionTokenRequest; | ||
import org.donggle.backend.application.service.oauth.notion.dto.NotionTokenResponse; | ||
import org.donggle.backend.application.service.request.OAuthAccessTokenRequest; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
@Service | ||
public class NotionOAuthService { | ||
public static final String AUTHORIZE_URL = "https://api.notion.com/v1/oauth/authorize"; | ||
public static final String TOKEN_URL = "https://api.notion.com/v1/oauth/token"; | ||
private static final String GRANT_TYPE = "authorization_code"; | ||
private static final String RESPONSE_TYPE = "code"; | ||
private static final String OWNER = "user"; | ||
|
||
private final String clientId; | ||
private final String clientSecret; | ||
private final WebClient webClient; | ||
|
||
public NotionOAuthService(@Value("${notion_client_id}") final String clientId, | ||
@Value("${notion_client_secret}") final String clientSecret) { | ||
this.clientId = clientId; | ||
this.clientSecret = clientSecret; | ||
this.webClient = WebClient.create(); | ||
} | ||
|
||
public String createRedirectUri(final String redirectUri) { | ||
return UriComponentsBuilder.fromUriString(AUTHORIZE_URL) | ||
.queryParam("client_id", clientId) | ||
.queryParam("redirect_uri", redirectUri) | ||
.queryParam("response_type", RESPONSE_TYPE) | ||
.queryParam("owner", OWNER) | ||
.build() | ||
.toUriString(); | ||
} | ||
|
||
public void getAccessToken(final OAuthAccessTokenRequest oAuthAccessTokenRequest) { | ||
final String redirectUri = oAuthAccessTokenRequest.redirectUri(); | ||
final String code = oAuthAccessTokenRequest.code(); | ||
|
||
NotionTokenResponse response = webClient.post() | ||
.uri(TOKEN_URL) | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
.header(HttpHeaders.AUTHORIZATION, "Basic " + base64Encode(clientId + ":" + clientSecret)) | ||
.bodyValue(new NotionTokenRequest(GRANT_TYPE, code, redirectUri)) | ||
.retrieve() | ||
.bodyToMono(NotionTokenResponse.class) | ||
.block(); | ||
} | ||
|
||
private String base64Encode(String value) { | ||
return java.util.Base64.getEncoder().encodeToString(value.getBytes()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...in/java/org/donggle/backend/application/service/oauth/notion/dto/NotionOwnerResponse.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,7 @@ | ||
package org.donggle.backend.application.service.oauth.notion.dto; | ||
|
||
public record NotionOwnerResponse( | ||
String type, | ||
NotionUserResponse user | ||
) { | ||
} |
4 changes: 4 additions & 0 deletions
4
...ain/java/org/donggle/backend/application/service/oauth/notion/dto/NotionTokenRequest.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,4 @@ | ||
package org.donggle.backend.application.service.oauth.notion.dto; | ||
|
||
public record NotionTokenRequest(String grant_type, String code, String redirect_uri) { | ||
} |
13 changes: 13 additions & 0 deletions
13
...in/java/org/donggle/backend/application/service/oauth/notion/dto/NotionTokenResponse.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 org.donggle.backend.application.service.oauth.notion.dto; | ||
|
||
public record NotionTokenResponse( | ||
String access_token, // accessToken | ||
String bot_id, // 승인에 대한 식별자 | ||
String duplicated_template_id, | ||
// 사용자 작업 공간에 생성된 새 페이지(개발자가 통합과 함께 제공한 템플릿의 복제본)의 ID, 개발자가 통합을 위한 템플릿을 제공하지 않은 경우 값은 null | ||
NotionOwnerResponse owner, // 이 통합을 보고 공유할 수 있는 사람에 대한 정보가 포함된 개체 | ||
String workspace_icon, // UI에서 이 승인을 표시하는 데 사용할 수 있는 이미지의 URL | ||
String workspace_id, // 이 승인이 발생한 워크스페이스의 ID | ||
String workspace_name // UI에서 이 승인을 표시하는 데 사용할 수 있는 사람이 읽을 수 있는 이름 | ||
) { | ||
} |
7 changes: 7 additions & 0 deletions
7
...ain/java/org/donggle/backend/application/service/oauth/notion/dto/NotionUserResponse.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,7 @@ | ||
package org.donggle.backend.application.service.oauth.notion.dto; | ||
|
||
public record NotionUserResponse( | ||
Long id, | ||
String obejct | ||
) { | ||
} |
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
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