Skip to content

Commit

Permalink
feat: support on sso authorize and auto_login (#53)
Browse files Browse the repository at this point in the history
* feat: support on sso authorize and auto_login

* update: update readme and comment

* feat: add Credentials: sso
  • Loading branch information
wzxha authored May 15, 2024
1 parent 055b37a commit eb63af8
Show file tree
Hide file tree
Showing 24 changed files with 286 additions and 163 deletions.
8 changes: 4 additions & 4 deletions AliyunpanSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
F447857D2BBAB9AE00FE8247 /* TestFile1.txt in Resources */ = {isa = PBXBuildFile; fileRef = F447857C2BBAB9AE00FE8247 /* TestFile1.txt */; };
F47FADDE2B14767D00EC0D8D /* AliyunpanSDK.swift in Sources */ = {isa = PBXBuildFile; fileRef = F47FADB72B14767D00EC0D8D /* AliyunpanSDK.swift */; };
F47FADDF2B14767D00EC0D8D /* AliyunpanPKCECredentials.swift in Sources */ = {isa = PBXBuildFile; fileRef = F47FADB92B14767D00EC0D8D /* AliyunpanPKCECredentials.swift */; };
F47FADE12B14767D00EC0D8D /* AliyunpanAppJumper.swift in Sources */ = {isa = PBXBuildFile; fileRef = F47FADBB2B14767D00EC0D8D /* AliyunpanAppJumper.swift */; };
F47FADE12B14767D00EC0D8D /* AliyunpanAuthenticator.swift in Sources */ = {isa = PBXBuildFile; fileRef = F47FADBB2B14767D00EC0D8D /* AliyunpanAuthenticator.swift */; };
F47FADE22B14767D00EC0D8D /* AliyunpanCredentials.swift in Sources */ = {isa = PBXBuildFile; fileRef = F47FADBC2B14767D00EC0D8D /* AliyunpanCredentials.swift */; };
F47FADE32B14767D00EC0D8D /* AliyunpanServerCredentials.swift in Sources */ = {isa = PBXBuildFile; fileRef = F47FADBD2B14767D00EC0D8D /* AliyunpanServerCredentials.swift */; };
F47FADE42B14767D00EC0D8D /* AliyunpanSDK.h in Headers */ = {isa = PBXBuildFile; fileRef = F47FADBE2B14767D00EC0D8D /* AliyunpanSDK.h */; };
Expand Down Expand Up @@ -103,7 +103,7 @@
F447857C2BBAB9AE00FE8247 /* TestFile1.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = TestFile1.txt; path = Tests/TestFile1.txt; sourceTree = SOURCE_ROOT; };
F47FADB72B14767D00EC0D8D /* AliyunpanSDK.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliyunpanSDK.swift; sourceTree = "<group>"; };
F47FADB92B14767D00EC0D8D /* AliyunpanPKCECredentials.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliyunpanPKCECredentials.swift; sourceTree = "<group>"; };
F47FADBB2B14767D00EC0D8D /* AliyunpanAppJumper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliyunpanAppJumper.swift; sourceTree = "<group>"; };
F47FADBB2B14767D00EC0D8D /* AliyunpanAuthenticator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliyunpanAuthenticator.swift; sourceTree = "<group>"; };
F47FADBC2B14767D00EC0D8D /* AliyunpanCredentials.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliyunpanCredentials.swift; sourceTree = "<group>"; };
F47FADBD2B14767D00EC0D8D /* AliyunpanServerCredentials.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AliyunpanServerCredentials.swift; sourceTree = "<group>"; };
F47FADBE2B14767D00EC0D8D /* AliyunpanSDK.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AliyunpanSDK.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -235,7 +235,7 @@
isa = PBXGroup;
children = (
F4E9F96B2B148AEE00DC8DBF /* AliyunpanMessage.swift */,
F47FADBB2B14767D00EC0D8D /* AliyunpanAppJumper.swift */,
F47FADBB2B14767D00EC0D8D /* AliyunpanAuthenticator.swift */,
F47FADBC2B14767D00EC0D8D /* AliyunpanCredentials.swift */,
F47FADBD2B14767D00EC0D8D /* AliyunpanServerCredentials.swift */,
F47FADB92B14767D00EC0D8D /* AliyunpanPKCECredentials.swift */,
Expand Down Expand Up @@ -570,7 +570,7 @@
F47FADE22B14767D00EC0D8D /* AliyunpanCredentials.swift in Sources */,
F47FADF32B14767D00EC0D8D /* AliyunpanToken.swift in Sources */,
F49821532B188A6D006559CC /* CreateFile.swift in Sources */,
F47FADE12B14767D00EC0D8D /* AliyunpanAppJumper.swift in Sources */,
F47FADE12B14767D00EC0D8D /* AliyunpanAuthenticator.swift in Sources */,
F49821482B188A6D006559CC /* GetUsersScopes.swift in Sources */,
F4BD1C5F2B32BFD9002BEA2A /* ThreadSafe.swift in Sources */,
F4BD1C482B304757002BEA2A /* AliyunpanDownloader.swift in Sources */,
Expand Down
12 changes: 6 additions & 6 deletions Demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@
);
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.smartdrive.Demo-visionOS";
PRODUCT_BUNDLE_IDENTIFIER = com.aliyunpanSDK.demo;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = xros;
SUPPORTED_PLATFORMS = "xros xrsimulator";
Expand Down Expand Up @@ -749,7 +749,7 @@
);
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.smartdrive.Demo-visionOS";
PRODUCT_BUNDLE_IDENTIFIER = com.aliyunpanSDK.demo;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = xros;
SUPPORTED_PLATFORMS = "xros xrsimulator";
Expand Down Expand Up @@ -782,7 +782,7 @@
);
MACOSX_DEPLOYMENT_TARGET = 13.3;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.smartdrive.AliyunpanSDK.demo.Demo-MacOS";
PRODUCT_BUNDLE_IDENTIFIER = com.aliyunpanSDK.demo;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down Expand Up @@ -812,7 +812,7 @@
);
MACOSX_DEPLOYMENT_TARGET = 13.3;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.smartdrive.AliyunpanSDK.demo.Demo-MacOS";
PRODUCT_BUNDLE_IDENTIFIER = com.aliyunpanSDK.demo;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -838,7 +838,7 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.smartdrive.AliyunpanSDK.demo.Demo-TVOS";
PRODUCT_BUNDLE_IDENTIFIER = com.aliyunpanSDK.demo;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = appletvos;
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down Expand Up @@ -866,7 +866,7 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.smartdrive.AliyunpanSDK.demo.Demo-TVOS";
PRODUCT_BUNDLE_IDENTIFIER = com.aliyunpanSDK.demo;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = appletvos;
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
8 changes: 8 additions & 0 deletions Demo/Demo/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import AliyunpanSDK
let appId = "YOUR_APP_ID" // 替换成你的 AppID
let scope = "user:base,file:all:read,file:all:write"

/// 调试参数
/// 环境
let environment = Aliyunpan.Environment.product
/// 日志等级
let logLevel = AliyunpanLogLevel.info
/// 运行时清空 token
let cleanToken = true

let client = AliyunpanClient(
appId: appId,
scope: scope
Expand Down
5 changes: 4 additions & 1 deletion Demo/Demo/Demo-MacOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import AliyunpanSDK
@main
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
Aliyunpan.setLogLevel(.info)
Aliyunpan.setEnvironment(environment)
Aliyunpan.setLogLevel(logLevel)

client.cleanToken()
}
}
47 changes: 36 additions & 11 deletions Demo/Demo/Demo-MacOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,35 @@ import Cocoa
import AliyunpanSDK

class ViewController: NSViewController {
private var authorizeButton = NSButton()
private var buttonContainer = NSStackView()
private var pkceButton = NSButton()
private var qrCodeButton = NSButton()

private var imageView = NSImageView()

override func viewDidLoad() {
super.viewDidLoad()
authorizeButton.title = "Authorize"
authorizeButton.bezelStyle = .roundRect
view.addSubview(authorizeButton)
authorizeButton.translatesAutoresizingMaskIntoConstraints = false

buttonContainer.orientation = .vertical
view.addSubview(buttonContainer)
buttonContainer.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
authorizeButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
authorizeButton.centerYAnchor.constraint(equalTo: view.centerYAnchor),
authorizeButton.widthAnchor.constraint(equalToConstant: 200),
authorizeButton.heightAnchor.constraint(equalToConstant: 60)
buttonContainer.centerXAnchor.constraint(equalTo: view.centerXAnchor),
buttonContainer.centerYAnchor.constraint(equalTo: view.centerYAnchor),
buttonContainer.widthAnchor.constraint(equalToConstant: 200),
buttonContainer.heightAnchor.constraint(equalToConstant: 60)
])
authorizeButton.action = #selector(showQRCode)

pkceButton.title = "Authorize(PKCE)"
pkceButton.bezelStyle = .roundRect
pkceButton.action = #selector(pkceAuthorize)

qrCodeButton.title = "Authorize(QR Code)"
qrCodeButton.bezelStyle = .roundRect
qrCodeButton.action = #selector(showQRCode)

buttonContainer.addArrangedSubview(pkceButton)
buttonContainer.addArrangedSubview(qrCodeButton)

view.addSubview(imageView)
imageView.isHidden = true
Expand All @@ -45,6 +58,18 @@ class ViewController: NSViewController {
}
}

@objc private func pkceAuthorize() {
Task {
do {
try await client.authorize(
credentials: .pkce)
navigateToExamples()
} catch {
print(error)
}
}
}

@objc private func showQRCode() {
Task {
do {
Expand Down Expand Up @@ -72,7 +97,7 @@ extension ViewController: AliyunpanQRCodeContainer {
}

func showAliyunpanAuthorizeQRCode(with url: URL) {
authorizeButton.isHidden = true
buttonContainer.isHidden = true
imageView.isHidden = false
Task {
let (data, _) = try await URLSession.shared.data(from: url)
Expand Down
5 changes: 4 additions & 1 deletion Demo/Demo/Demo-iOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import AliyunpanSDK
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationDidFinishLaunching(_ application: UIApplication) {
Aliyunpan.setLogLevel(.info)
Aliyunpan.setEnvironment(environment)
Aliyunpan.setLogLevel(logLevel)

client.cleanToken()
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
Expand Down
4 changes: 0 additions & 4 deletions Demo/Demo/Demo-iOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>smartdrive</string>
</array>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
Expand Down
44 changes: 9 additions & 35 deletions Demo/Demo/Demo-visionOS/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,25 @@ import RealityKitContent
import AliyunpanSDK

struct AuthorizeView: View {
@State var image = UIImage()
@State var status: AliyunpanAuthorizeQRCodeStatus?
@State var userInfo: AliyunpanScope.User.GetUsersInfo.Response?

var isAuthorizing: Bool {
status != nil && status != .scanSuccess
}

var isAuthorized: Bool {
userInfo != nil
}

var showAuthorizeButton: Bool {
guard !isAuthorized else {
return false
}
return !isAuthorizing
}

var body: some View {
Image(uiImage: image)
.padding(.bottom, 50)

if showAuthorizeButton {
if !isAuthorized {
Button("Authorize") {
Task {
let userInfo = try await client.authorize(
credentials: .qrCode(self))
.send(AliyunpanScope.User.GetUsersInfo())
self.userInfo = userInfo
do {
let userInfo = try await client.authorize(
credentials: .pkce)
.send(AliyunpanScope.User.GetUsersInfo())
self.userInfo = userInfo
} catch {
print(error)
}
}
}
.font(.extraLargeTitle)
Expand All @@ -54,21 +43,6 @@ struct AuthorizeView: View {
}
}

extension AuthorizeView: AliyunpanQRCodeContainer {
func showAliyunpanAuthorizeQRCode(with url: URL) {
Task {
let (data, _) = try await URLSession.shared.data(from: url)
if let image = UIImage(data: data) {
self.image = image
}
}
}

func authorizeQRCodeStatusUpdated(_ status: AliyunpanSDK.AliyunpanAuthorizeQRCodeStatus) {
self.status = status
}
}

struct ContentView: View {
@State var authorizeView = AuthorizeView()

Expand Down
14 changes: 14 additions & 0 deletions Demo/Demo/Demo-visionOS/Demo_visionOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@
//

import SwiftUI
import AliyunpanSDK

class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
Aliyunpan.setEnvironment(environment)
Aliyunpan.setLogLevel(logLevel)

client.cleanToken()

return true
}
}

@main
struct Demo_visionOSApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

var body: some Scene {
WindowGroup {
ContentView()
Expand Down
4 changes: 2 additions & 2 deletions Demo/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- AliyunpanSDK (0.2.0)
- AliyunpanSDK (0.2.1)

DEPENDENCIES:
- AliyunpanSDK (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
AliyunpanSDK: 9677ed52f2519f512e91bf3097438c35772535e3
AliyunpanSDK: 8611714269b39ec8e2c41427f9b77a08fd85fc9b

PODFILE CHECKSUM: 40334940ba0e4f081833162ea871b81051e79c17

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ let client: AliyunpanClient = AliyunpanClient(

| 授权方式 | 描述 | **不需要** Server | **不需要**阿里云盘客户端 |
| :----: | :----: | :----: | :----: |
| pkce | pkce 授权 |||
| server | 业务后端授权 |||
| pkce | pkce 授权 |||
| sso | sso pkce 授权 |||
| server | 业务后端授权 |||
| qrCode | 二维码授权 |||
| token | 注入 token 授权 |||

Expand Down Expand Up @@ -131,7 +132,7 @@ downloadr.addDelegate(DELEGATE)

```ruby
target 'MyApp' do
pod 'AliyunpanSDK', '~> 0.1'
pod 'AliyunpanSDK', '~> 0.2'
end
```

Expand Down
4 changes: 2 additions & 2 deletions Sources/AliyunpanSDK/AliyunpanClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public class AliyunpanClient {
appId: config.appId,
scope: config.scope
)
await MainActor.run { [weak self] in
self?.token = token
await MainActor.run {
self.token = token
}
return token
}
Expand Down
Loading

0 comments on commit eb63af8

Please sign in to comment.