Skip to content

Commit 1c218df

Browse files
committed
refactor: Apple 인증을 REST API로 이전
1 parent 5736509 commit 1c218df

5 files changed

Lines changed: 289 additions & 185 deletions

File tree

Application/Infra/Sources/Service/FunctionAPIClient.swift

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ final class FunctionAPIClient {
7979
_ endpoint: FunctionAPIEndpoint<Response>,
8080
requiresAuthentication: Bool = true
8181
) async throws -> Response {
82-
try await send(
83-
endpoint,
84-
payload: EmptyPayload(),
85-
requiresAuthentication: requiresAuthentication
86-
)
82+
var request = try client()
83+
.request(endpoint)
84+
85+
if requiresAuthentication {
86+
request = request.authorized()
87+
}
88+
89+
return try await request.send()
8790
}
8891

8992
private func client() throws -> NXAPIClient {
@@ -96,16 +99,36 @@ struct FunctionAPIEndpoint<Response: Decodable>: NXEndpoint {
9699
let path: String
97100
}
98101

99-
struct FunctionAPIResponse: Decodable {
102+
struct GithubAuthenticationResponse: Decodable {
100103
let accessToken: String?
101104
let customToken: String?
102-
let refreshToken: String?
103-
let token: String?
104105
}
105106

106-
struct EmptyAPIResponse: Decodable {}
107+
struct AppleChallengeResponse: Decodable {
108+
let challengeId: String
109+
let hashedNonce: String
110+
}
111+
112+
struct AppleCustomTokenResponse: Decodable {
113+
let customToken: String
114+
}
115+
116+
struct AppleOperationResponse: Decodable {
117+
let success: Bool
118+
}
119+
120+
struct AppleCustomTokenRequest: Encodable {
121+
let challengeId: String
122+
let authorizationCode: String
123+
}
107124

108-
private struct EmptyPayload: Encodable {}
125+
struct AppleAccountLinkRequest: Encodable {
126+
let challengeId: String
127+
let authorizationCode: String
128+
let credentialEmail: String?
129+
}
130+
131+
struct EmptyAPIResponse: Decodable {}
109132

110133
private struct FunctionAPIErrorBody: Decodable {
111134
let code: String
@@ -116,9 +139,16 @@ private enum FunctionAPIErrorCode: String {
116139
case emailNotFound = "email-not-found"
117140
case emailMismatch = "email-mismatch"
118141
case githubEmailConflict = "github-email-changed-account-conflict"
142+
case appleProviderLinkConflict = "apple-provider-link-conflict"
143+
case lastProvider = "last-provider"
119144
}
120145

121-
private struct FunctionAPIServerErrorDecoder: NXServerErrorDecoder {
146+
enum AppleAuthenticationAPIError: Error, Equatable {
147+
case providerLinkConflict
148+
case lastProvider
149+
}
150+
151+
struct FunctionAPIServerErrorDecoder: NXServerErrorDecoder {
122152
func decodeServerError(
123153
data: Data,
124154
response: HTTPURLResponse,
@@ -136,6 +166,10 @@ private struct FunctionAPIServerErrorDecoder: NXServerErrorDecoder {
136166
return EmailError.mismatch
137167
case .githubEmailConflict:
138168
return EmailError.githubEmailConflict
169+
case .appleProviderLinkConflict:
170+
return AppleAuthenticationAPIError.providerLinkConflict
171+
case .lastProvider:
172+
return AppleAuthenticationAPIError.lastProvider
139173
}
140174
}
141175
}
@@ -152,13 +186,19 @@ private actor FirebaseAuthTokenProvider: NXAuthTokenProvider {
152186

153187
extension Error {
154188
var apiEmailError: EmailError? {
189+
functionAPIUnderlyingError as? EmailError
190+
}
191+
192+
var apiAppleAuthenticationError: AppleAuthenticationAPIError? {
193+
functionAPIUnderlyingError as? AppleAuthenticationAPIError
194+
}
195+
196+
private var functionAPIUnderlyingError: (any Error)? {
155197
guard let error = self as? NXError,
156-
case let .server(
157-
statusCode: _,
158-
data: _,
159-
underlying: underlying
160-
) = error else { return nil }
198+
case let .server(statusCode: _, data: _, underlying: underlying) = error else {
199+
return nil
200+
}
161201

162-
return underlying as? EmailError
202+
return underlying
163203
}
164204
}

Application/Infra/Sources/Service/FunctionAPIEndpoint.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,24 @@ extension FunctionAPIEndpoint where Response == EmptyAPIResponse {
3434
Self(method: .delete, path: "/push-notifications/\(functionAPIPathSegment(id))/deletion-request")
3535
}
3636

37-
static let revokeAppleAccessToken = Self(method: .delete, path: "/auth/apple/access-token")
3837
static let revokeGithubAccessToken = Self(method: .delete, path: "/auth/github/access-token")
3938
}
4039

41-
extension FunctionAPIEndpoint where Response == FunctionAPIResponse {
40+
extension FunctionAPIEndpoint where Response == AppleChallengeResponse {
41+
static let requestAppleChallenge = Self(method: .post, path: "/auth/apple/challenges")
42+
}
43+
44+
extension FunctionAPIEndpoint where Response == AppleCustomTokenResponse {
4245
static let requestAppleCustomToken = Self(method: .post, path: "/auth/apple/custom-token")
43-
static let refreshAppleAccessToken = Self(method: .post, path: "/auth/apple/access-token")
44-
static let requestAppleRefreshToken = Self(method: .post, path: "/auth/apple/refresh-token")
46+
}
47+
48+
extension FunctionAPIEndpoint where Response == AppleOperationResponse {
49+
static let linkAppleAccount = Self(method: .put, path: "/auth/apple/account-link")
50+
static let unlinkAppleAccount = Self(method: .delete, path: "/auth/apple/account-link")
51+
static let revokeAppleAccessToken = Self(method: .delete, path: "/auth/apple/access-token")
52+
}
53+
54+
extension FunctionAPIEndpoint where Response == GithubAuthenticationResponse {
4555
static let linkGithubProvider = Self(method: .post, path: "/auth/github/link")
4656
static let requestGithubTokens = Self(method: .post, path: "/auth/github/tokens")
4757
}

Application/Infra/Sources/Service/SocialLogin/AppleAuthResponse.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
//
77

88
import Foundation
9-
import AuthenticationServices
109

1110
struct AppleAuthResponse {
12-
let nonce: String
13-
let credential: ASAuthorizationAppleIDCredential
14-
let authorizationCode: Data
15-
let idTokenString: String
11+
let authorizationCode: String
12+
let fullName: PersonNameComponents?
13+
let email: String?
1614
}

0 commit comments

Comments
 (0)