Skip to content

Commit

Permalink
feat: add new authentication method to allow manual token injection (#28
Browse files Browse the repository at this point in the history
)
  • Loading branch information
wzxha authored Jan 16, 2024
1 parent cabf144 commit cced4d3
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 13 deletions.
12 changes: 11 additions & 1 deletion AliyunpanSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
F498215C2B188A6D006559CC /* GetVipFeatureList.swift in Sources */ = {isa = PBXBuildFile; fileRef = F498213D2B188A6D006559CC /* GetVipFeatureList.swift */; };
F498215E2B188A6D006559CC /* GetVipFeatureTrial.swift in Sources */ = {isa = PBXBuildFile; fileRef = F498213F2B188A6D006559CC /* GetVipFeatureTrial.swift */; };
F49821602B188C4C006559CC /* AliyunpanSpaceInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = F498215F2B188C4C006559CC /* AliyunpanSpaceInfo.swift */; };
F4A37F1C2B562081009AC6AA /* AliyunpanTokenCredentials.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4A37F1B2B562081009AC6AA /* AliyunpanTokenCredentials.swift */; };
F4B3F7332B29859100D9E122 /* CredentialTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4B3F7322B29859100D9E122 /* CredentialTests.swift */; };
F4BD1B642B29A11B002BEA2A /* Platform.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4BD1B632B29A11B002BEA2A /* Platform.swift */; };
F4BD1C322B2AAB07002BEA2A /* AliyunpanQRCodeCredentials.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4BD1C312B2AAB07002BEA2A /* AliyunpanQRCodeCredentials.swift */; };
Expand Down Expand Up @@ -151,6 +152,7 @@
F498213D2B188A6D006559CC /* GetVipFeatureList.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetVipFeatureList.swift; sourceTree = "<group>"; };
F498213F2B188A6D006559CC /* GetVipFeatureTrial.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetVipFeatureTrial.swift; sourceTree = "<group>"; };
F498215F2B188C4C006559CC /* AliyunpanSpaceInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AliyunpanSpaceInfo.swift; sourceTree = "<group>"; };
F4A37F1B2B562081009AC6AA /* AliyunpanTokenCredentials.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AliyunpanTokenCredentials.swift; sourceTree = "<group>"; };
F4B3F7142B202CB500D9E122 /* DownloaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloaderTests.swift; sourceTree = "<group>"; };
F4B3F7322B29859100D9E122 /* CredentialTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CredentialTests.swift; sourceTree = "<group>"; };
F4BD1B632B29A11B002BEA2A /* Platform.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Platform.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -231,6 +233,7 @@
F47FADBD2B14767D00EC0D8D /* AliyunpanServerCredentials.swift */,
F47FADB92B14767D00EC0D8D /* AliyunpanPKCECredentials.swift */,
F4BD1C312B2AAB07002BEA2A /* AliyunpanQRCodeCredentials.swift */,
F4A37F1B2B562081009AC6AA /* AliyunpanTokenCredentials.swift */,
);
path = AliyunpanCredentials;
sourceTree = "<group>";
Expand Down Expand Up @@ -468,7 +471,7 @@
attributes = {
BuildIndependentTargetsInParallel = 1;
LastSwiftUpdateCheck = 1430;
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1520;
TargetAttributes = {
F4C6F2252B060C4B003A06B3 = {
CreatedOnToolsVersion = 14.3.1;
Expand Down Expand Up @@ -567,6 +570,7 @@
F49821462B188A6D006559CC /* GetUsersInfo.swift in Sources */,
F49821422B188A6D006559CC /* UpdateVideoRecord.swift in Sources */,
F49821582B188A6D006559CC /* GetFileList.swift in Sources */,
F4A37F1C2B562081009AC6AA /* AliyunpanTokenCredentials.swift in Sources */,
F47FADDE2B14767D00EC0D8D /* AliyunpanSDK.swift in Sources */,
F49821402B188A6D006559CC /* GetVideoPreviewPlayMeta.swift in Sources */,
F49821442B188A6D006559CC /* GetVipInfo.swift in Sources */,
Expand Down Expand Up @@ -670,6 +674,7 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
Expand Down Expand Up @@ -735,6 +740,7 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
Expand Down Expand Up @@ -766,6 +772,7 @@
CLANG_ENABLE_CODE_COVERAGE = YES;
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEFINES_MODULE = NO;
DEVELOPMENT_TEAM = "";
DYLIB_COMPATIBILITY_VERSION = 1;
Expand Down Expand Up @@ -812,6 +819,7 @@
CLANG_ENABLE_CODE_COVERAGE = YES;
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEFINES_MODULE = NO;
DEVELOPMENT_TEAM = "";
DYLIB_COMPATIBILITY_VERSION = 1;
Expand Down Expand Up @@ -860,6 +868,7 @@
CLANG_UNDEFINED_BEHAVIOR_SANITIZER_NULLABILITY = YES;
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = "";
GCC_GENERATE_TEST_COVERAGE_FILES = NO;
GENERATE_INFOPLIST_FILE = NO;
Expand Down Expand Up @@ -888,6 +897,7 @@
CLANG_UNDEFINED_BEHAVIOR_SANITIZER_NULLABILITY = YES;
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = "";
GCC_GENERATE_TEST_COVERAGE_FILES = NO;
GENERATE_INFOPLIST_FILE = NO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1520"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
| :----: | :----: | :----: | :----: |
| pkce | pkce 授权 |||
| server | 业务后端授权 |||
| qrCode | 二维码授权 |||
| qrCode | 二维码授权 |||
| token | 注入 token 授权 |||

```swift
let client: AliyunpanClient = AliyunpanClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public enum AliyunpanCredentials {
case server(AliyunpanBizServer)
/// 二维码授权,无需服务端,无需安装阿里云盘客户端
case qrCode(AliyunpanQRCodeContainer)
/// 手动注入 token
case token(AliyunpanToken)

var implement: AliyunpanCredentialsProtocol {
switch self {
Expand All @@ -28,6 +30,8 @@ public enum AliyunpanCredentials {
return AliyunpanServerCredentials(server)
case .qrCode(let container):
return AliyunpanQRCodeCredentials(container)
case .token(let token):
return AliyunpanTokenCredentials(token)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// AliyunpanTokenCredentials.swift
// AliyunpanSDK
//
// Created by zhaixian on 2024/1/16.
//

import Foundation

class AliyunpanTokenCredentials: AliyunpanCredentialsProtocol {
let token: AliyunpanToken

init(_ token: AliyunpanToken) {
self.token = token
}

func authorize(appId: String, scope: String) async throws -> AliyunpanToken {
token
}
}
11 changes: 11 additions & 0 deletions Sources/AliyunpanSDK/Model/AliyunpanToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ public struct AliyunpanToken: Codable {
public var isExpired: Bool {
Date().timeIntervalSince1970 > expires_in
}

public init(
token_type: String = "Bearer",
access_token: String,
refresh_token: String? = nil,
expires_in: TimeInterval = .greatestFiniteMagnitude) {
self.token_type = token_type
self.access_token = access_token
self.refresh_token = refresh_token
self.expires_in = expires_in
}
}

extension AliyunpanToken: Hashable, Equatable {
Expand Down
2 changes: 1 addition & 1 deletion Tests/AliyunpanSDKTests/AliyunpanSDKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AliyunpanSDKTests: XCTestCase {

let url1 = URL(string: "smartdrive123456://authorize?state=abc&code=anycode")!
XCTAssertTrue(Aliyunpan.handleOpenURL(url1))
let url2 = URL(string: "123456://authorize?state=abc&code=anycode")!
let url2 = URL(string: "taobao://authorize?state=abc&code=anycode")!
XCTAssertFalse(Aliyunpan.handleOpenURL(url2))
}
}
1 change: 1 addition & 0 deletions Tests/AliyunpanSDKTests/CredentialTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ class CredentialTests: XCTestCase {
XCTAssertTrue(AliyunpanCredentials.pkce.implement is AliyunpanPKCECredentials)
XCTAssertTrue(AliyunpanCredentials.server(TestBizServer()).implement is AliyunpanServerCredentials)
XCTAssertTrue(AliyunpanCredentials.qrCode(TestQRCodeContainer()).implement is AliyunpanQRCodeCredentials)
XCTAssertTrue(AliyunpanCredentials.token(.init(access_token: "accessToken")).implement is AliyunpanTokenCredentials)
}
}
6 changes: 3 additions & 3 deletions Tests/AliyunpanSDKTests/DownloaderTaskTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class DownloaderActorTests: XCTestCase, AliyunpanDownloadTaskDelegate {

XCTAssertEqual(getDownloadURLCount, 1)

try await Task.sleep(seconds: 1)
try await Task.sleep(seconds: 0.06)

_ = try await actor.getDownloadURL(with: file, by: self)
_ = try await actor.getDownloadURL(with: file, by: self)
Expand All @@ -115,13 +115,13 @@ class DownloaderActorTests: XCTestCase, AliyunpanDownloadTaskDelegate {
}

func getFileDownloadUrl(driveId: String, fileId: String) async throws -> AliyunpanScope.File.GetFileDownloadUrl.Response {
try await Task.sleep(seconds: 0.5)
try await Task.sleep(seconds: 0.05)

getDownloadURLCount += 1

return AliyunpanScope.File.GetFileDownloadUrl.Response(
url: URL(string: "https://alipan.com")!,
expiration: Date().addingTimeInterval(1),
expiration: Date().addingTimeInterval(0.05),
method: "GET")
}

Expand Down
12 changes: 6 additions & 6 deletions Tests/AliyunpanSDKTests/TaskTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ import XCTest
class TaskTests: XCTestCase {
func testTimeout() async throws {
do {
let _ = try await withTimeout(seconds: 2) {
try await Task.sleep(nanoseconds: UInt64(3 * 1_000_000_000))
let _ = try await withTimeout(seconds: 0.05) {
try await Task.sleep(seconds: 0.1)
return 1
}
} catch {
XCTAssertTrue(error is CancellationError)
}

let value1 = try await withTimeout(seconds: 2) {
let value1 = try await withTimeout(seconds: 0.05) {
Task {
try await Task.sleep(nanoseconds: UInt64(2 * 1_000_000_000))
try await Task.sleep(seconds: 0.05)
return 1
}
}.value
XCTAssertEqual(1, value1)

let value2 = try await withTimeout(seconds: 2) {
let value2 = try await withTimeout(seconds: 0.05) {
Task {
try await Task.sleep(nanoseconds: UInt64(1 * 1_000_000_000))
try await Task.sleep(seconds: 0.025)
return 1
}
}.value
Expand Down

0 comments on commit cced4d3

Please sign in to comment.