-
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.
Merge pull request #1 from team-onui/feture
🔀 :: feture to develop
- Loading branch information
Showing
59 changed files
with
543 additions
and
371 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 |
---|---|---|
|
@@ -40,4 +40,4 @@ out/ | |
.env | ||
|
||
### copose ### | ||
Docker-compose.yml | ||
Docker-compose.yml |
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
32 changes: 17 additions & 15 deletions
32
src/main/kotlin/com/example/onui/domain/auth/presentation/AuthController.kt
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 |
---|---|---|
@@ -1,34 +1,36 @@ | ||
package com.example.onui.domain.auth.presentation | ||
|
||
import com.example.onui.domain.auth.presentation.dto.request.ReissueRequest | ||
import com.example.onui.domain.auth.presentation.dto.response.OauthLinkResponse | ||
import com.example.onui.domain.auth.presentation.dto.response.TokenResponse | ||
import com.example.onui.domain.auth.service.AppleAuthService | ||
import com.example.onui.domain.auth.service.AuthService | ||
import com.example.onui.domain.auth.service.GoogleAuthService | ||
import org.springframework.validation.annotation.Validated | ||
import org.springframework.web.bind.annotation.* | ||
import javax.validation.Valid | ||
|
||
@Validated | ||
@RestController | ||
@RequestMapping("/auth") | ||
class AuthController( | ||
private val googleAuthService: GoogleAuthService, | ||
private val authService: AuthService | ||
private val authService: AuthService, | ||
private val appleAuthService: AppleAuthService | ||
) { | ||
|
||
@GetMapping("/google/link") | ||
fun getGoogleClientId(): OauthLinkResponse = googleAuthService.getGoogleLoginLink() | ||
|
||
@GetMapping("/oauth/google/token") | ||
@GetMapping("/google") | ||
fun oauthSignIn( | ||
@RequestParam(name = "code", required = true) | ||
code: String | ||
): TokenResponse = googleAuthService.oauthGoogleSignIn(code) | ||
@RequestParam(name = "token", required = true) | ||
token: String | ||
): TokenResponse = googleAuthService.oauthGoogleSignIn(token) | ||
|
||
@PutMapping("/token") | ||
@PutMapping("/re-issue") | ||
fun reissue( | ||
@RequestBody @Valid | ||
req: ReissueRequest | ||
): TokenResponse = authService.reissue(req.refreshToken!!) | ||
@RequestParam("token", required = true) | ||
token: String | ||
): TokenResponse = authService.reissue(token) | ||
|
||
@PostMapping("/apple") | ||
fun oauthSignInWithApple( | ||
@RequestParam(name = "token", required = true) | ||
token: String | ||
) = appleAuthService.signUp(token) | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/example/onui/domain/auth/presentation/dto/request/AppleTokenRequest.kt
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,9 @@ | ||
package com.example.onui.domain.auth.presentation.dto.request | ||
|
||
data class AppleTokenRequest( | ||
val clientId: String, | ||
val clientSecret: String, | ||
val code: String, | ||
val redirectUri: String, | ||
val grantType: String = "authorization_code" | ||
) |
5 changes: 0 additions & 5 deletions
5
src/main/kotlin/com/example/onui/domain/auth/presentation/dto/request/CodeRequest.kt
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
src/main/kotlin/com/example/onui/domain/auth/presentation/dto/request/GoogleCodeRequest.kt
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/example/onui/domain/auth/service/AppleAuthService.kt
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,6 @@ | ||
package com.example.onui.domain.auth.service | ||
|
||
interface AppleAuthService { | ||
|
||
fun signUp(idToken: String): Any | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/kotlin/com/example/onui/domain/auth/service/AppleAuthServiceImpl.kt
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,94 @@ | ||
package com.example.onui.domain.auth.service | ||
|
||
import com.example.onui.domain.auth.presentation.dto.response.TokenResponse | ||
import com.example.onui.domain.user.entity.User | ||
import com.example.onui.domain.user.repository.UserRepository | ||
import com.example.onui.global.config.error.exception.ExpiredTokenException | ||
import com.example.onui.global.config.error.exception.InvalidTokenException | ||
import com.example.onui.global.config.jwt.AppleJwtParser | ||
import com.example.onui.global.config.jwt.TokenProvider | ||
import com.example.onui.infra.feign.apple.AppleClient | ||
import com.example.onui.infra.feign.apple.dto.ApplePublicKey | ||
import com.example.onui.infra.feign.apple.dto.ApplePublicKeys | ||
import io.jsonwebtoken.Claims | ||
import io.jsonwebtoken.ExpiredJwtException | ||
import io.jsonwebtoken.Jwts | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
import java.security.KeyFactory | ||
import java.security.NoSuchAlgorithmException | ||
import java.security.PublicKey | ||
import java.security.spec.InvalidKeySpecException | ||
import java.util.* | ||
|
||
|
||
@Service | ||
class AppleAuthServiceImpl( | ||
private val appleClient: AppleClient, | ||
private val jwtProvider: TokenProvider, | ||
private val jwtParser: AppleJwtParser, | ||
private val userRepository: UserRepository | ||
) : AppleAuthService { | ||
|
||
private companion object { | ||
const val ALG_HEADER_KEY = "alg" | ||
const val KID_HEADER_KEY = "kid" | ||
const val NAME = "user-" | ||
} | ||
|
||
@Transactional | ||
override fun signUp(idToken: String): TokenResponse { | ||
|
||
val token = parseIdToken(idToken) | ||
|
||
val sub = token.subject | ||
|
||
|
||
val user = userRepository.findBySub(sub) | ||
?: userRepository.save( | ||
User( | ||
sub, | ||
token.get("email", String::class.java) | ||
?: (NAME + UUID.randomUUID().toString().replace("-", "")) | ||
) | ||
) | ||
|
||
return jwtProvider.receiveToken(user.sub) | ||
} | ||
|
||
private fun parseIdToken(idToken: String) = parseClaims( | ||
idToken, generatePublicKey(jwtParser.parseHeaders(idToken), appleClient.applePublicKeys()) | ||
) | ||
|
||
private fun generatePublicKey( | ||
tokenHeaders: MutableMap<String?, String?>, | ||
applePublicKeys: ApplePublicKeys | ||
): PublicKey { | ||
|
||
val publicKey: ApplePublicKey = | ||
applePublicKeys.matchesKey(tokenHeaders[ALG_HEADER_KEY]!!, tokenHeaders[KID_HEADER_KEY]!!) | ||
?: throw InvalidTokenException | ||
|
||
return try { | ||
KeyFactory.getInstance(publicKey.kty).generatePublic(publicKey.publicKeySpec()) | ||
} catch (e: NoSuchAlgorithmException) { | ||
throw IllegalStateException("Apple OAuth 로그인 중 public key 생성에 문제가 발생했습니다.") | ||
} catch (e: InvalidKeySpecException) { | ||
throw IllegalStateException("Apple OAuth 로그인 중 public key 생성에 문제가 발생했습니다.") | ||
} | ||
} | ||
|
||
private fun parseClaims(token: String, publicKey: PublicKey): Claims { | ||
return try { | ||
Jwts.parser() | ||
.setSigningKey(publicKey) | ||
.parseClaimsJws(token) | ||
.body | ||
} catch (e: Exception) { | ||
when (e) { | ||
is ExpiredJwtException -> throw ExpiredTokenException | ||
else -> throw InvalidTokenException | ||
} | ||
} | ||
} | ||
} |
5 changes: 1 addition & 4 deletions
5
src/main/kotlin/com/example/onui/domain/auth/service/GoogleAuthService.kt
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 |
---|---|---|
@@ -1,11 +1,8 @@ | ||
package com.example.onui.domain.auth.service | ||
|
||
import com.example.onui.domain.auth.presentation.dto.response.OauthLinkResponse | ||
import com.example.onui.domain.auth.presentation.dto.response.TokenResponse | ||
|
||
interface GoogleAuthService { | ||
|
||
fun getGoogleLoginLink(): OauthLinkResponse | ||
|
||
fun oauthGoogleSignIn(code: String): TokenResponse | ||
fun oauthGoogleSignIn(token: String): TokenResponse | ||
} |
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
Oops, something went wrong.