Skip to content

Commit

Permalink
feat: Update example with token swap
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Jan 15, 2024
1 parent 6f5e7ec commit edfb0de
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 11 deletions.
2 changes: 1 addition & 1 deletion login-Example/login-Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
SimpleResolver.sharedResolver.store(factory: Factory(type: InfomaniakLoginable.self) { _, _ in
let clientId = "9473D73C-C20F-4971-9E10-D957C563FA68"
let redirectUri = "com.infomaniak.drive://oauth2redirect"
let login = InfomaniakLogin(config: .init(clientId: clientId, redirectURI: redirectUri))
let login = InfomaniakLogin(config: .init(clientId: clientId, redirectURI: redirectUri, accessType: .none))
return login
})

Expand Down
24 changes: 18 additions & 6 deletions login-Example/login-Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="20037" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="20020"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22504"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand All @@ -18,7 +18,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="center" spacing="50" translatesAutoresizingMaskIntoConstraints="NO" id="0CE-Ne-b8B">
<rect key="frame" x="81.5" y="273" width="251" height="350"/>
<rect key="frame" x="81.5" y="223" width="251" height="450"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="0PN-Ed-zq1">
<rect key="frame" x="40" y="0.0" width="171" height="50"/>
Expand All @@ -32,8 +32,20 @@
<action selector="deleteAccount:" destination="BYZ-38-t0r" eventType="touchUpInside" id="29f-93-9Ur"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="GS5-id-qaL">
<rect key="frame" x="1" y="100" width="249" height="50"/>
<color key="backgroundColor" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="50" id="Wqj-II-BwR"/>
</constraints>
<fontDescription key="fontDescription" type="system" weight="semibold" pointSize="25"/>
<state key="normal" title="Refresh token convert"/>
<connections>
<action selector="refreshTokenConvert:" destination="BYZ-38-t0r" eventType="touchUpInside" id="raZ-wQ-Xh3"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ktV-So-Uo4">
<rect key="frame" x="0.0" y="100" width="251" height="50"/>
<rect key="frame" x="0.0" y="200" width="251" height="50"/>
<color key="backgroundColor" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="50" id="4Y2-O1-mjW"/>
Expand All @@ -45,7 +57,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="1z3-36-35t">
<rect key="frame" x="77" y="200" width="97" height="50"/>
<rect key="frame" x="77" y="300" width="97" height="50"/>
<color key="backgroundColor" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="50" id="tD2-Zq-lad"/>
Expand All @@ -57,7 +69,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="WdN-Au-grg">
<rect key="frame" x="21.5" y="300" width="208" height="50"/>
<rect key="frame" x="21.5" y="400" width="208" height="50"/>
<color key="backgroundColor" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="50" id="b5V-Ql-wFQ"/>
Expand Down
96 changes: 92 additions & 4 deletions login-Example/login-Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import AuthenticationServices
import InfomaniakDI
@testable import InfomaniakDI
import InfomaniakLogin
import UIKit

Expand Down Expand Up @@ -66,8 +66,10 @@ class ViewController: UIViewController, InfomaniakLoginDelegate, DeleteAccountDe
self.tokenService.getApiTokenUsing(code: code, codeVerifier: verifier) { token, _ in
if let token = token {
DispatchQueue.main.async {
let deleteAccountViewController = DeleteAccountViewController.instantiateInViewController(delegate: self,
accessToken: token.accessToken)
let deleteAccountViewController = DeleteAccountViewController.instantiateInViewController(
delegate: self,
accessToken: token.accessToken
)
self.present(deleteAccountViewController, animated: true)
}
}
Expand Down Expand Up @@ -100,7 +102,7 @@ class ViewController: UIViewController, InfomaniakLoginDelegate, DeleteAccountDe
@IBAction func asWebAuthentication(_ sender: Any) {
if #available(iOS 13.0, *) {
loginService.asWebAuthenticationLoginFrom(anchor: ASPresentationAnchor(),
useEphemeralSession: true,
useEphemeralSession: false,
hideCreateAccountButton: true,
delegate: self)
}
Expand All @@ -113,4 +115,90 @@ class ViewController: UIViewController, InfomaniakLoginDelegate, DeleteAccountDe
})
present(alertController, animated: true, completion: nil)
}

@IBAction func refreshTokenConvert(_ sender: Any) {
if #available(iOS 13.0, *) {
SimpleResolver.sharedResolver.removeAll()
// Init with non infinite refresh token
SimpleResolver.sharedResolver.store(factory: Factory(type: InfomaniakLoginable.self) { _, _ in
let clientId = "9473D73C-C20F-4971-9E10-D957C563FA68"
let redirectUri = "com.infomaniak.drive://oauth2redirect"
let login = InfomaniakLogin(config: .init(clientId: clientId, redirectURI: redirectUri, accessType: .offline))
return login
})
SimpleResolver.sharedResolver.store(factory: Factory(type: InfomaniakTokenable.self) { _, resolver in
return try resolver.resolve(type: InfomaniakLoginable.self,
forCustomTypeIdentifier: nil,
factoryParameters: nil,
resolver: resolver)
})

@InjectService var loginService: InfomaniakLoginable
@InjectService var tokenService: InfomaniakTokenable

loginService.asWebAuthenticationLoginFrom(anchor: .init(),
useEphemeralSession: false,
hideCreateAccountButton: true) { result in
switch result {
case .success(let success):
self.tokenService.getApiTokenUsing(code: success.code, codeVerifier: success.verifier) { token, error in
var title: String?
var description: String?

if let token = token {
title = "Login completed"
description = "UserId: \(token.userId)\nToken: \(token.accessToken)\nExpires in: \(token.expiresIn ?? -1)"
self.testSwapRefreshToken(apiToken: token)
} else if let error = error {
title = "Login error"
description = error.localizedDescription
}
print("refreshTokenConvert \(title ?? "")\n\(description ?? "")")
}
case .failure(let failure):
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.showAlert(title: "Error", message: failure.localizedDescription)
}
}
}
}
}

func testSwapRefreshToken(apiToken: ApiToken) {
SimpleResolver.sharedResolver.removeAll()
// Init with infinite refresh token
SimpleResolver.sharedResolver.store(factory: Factory(type: InfomaniakLoginable.self) { _, _ in
let clientId = "9473D73C-C20F-4971-9E10-D957C563FA68"
let redirectUri = "com.infomaniak.drive://oauth2redirect"
let login = InfomaniakLogin(config: .init(clientId: clientId, redirectURI: redirectUri, accessType: .none))
return login
})
SimpleResolver.sharedResolver.store(factory: Factory(type: InfomaniakTokenable.self) { _, resolver in
return try resolver.resolve(type: InfomaniakLoginable.self,
forCustomTypeIdentifier: nil,
factoryParameters: nil,
resolver: resolver)
})

@InjectService var loginService: InfomaniakLoginable
@InjectService var tokenService: InfomaniakTokenable

tokenService.refreshToken(token: apiToken) { token, error in
var title: String?
var description: String?
if let token = token {
title = "Login completed"
description = "UserId: \(token.userId)\nToken: \(token.accessToken)\nExpires in: \(token.expiresIn ?? -1)"
} else if let error = error {
title = "Login error"
description = error.localizedDescription
}
if let title = title,
let description = description {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.showAlert(title: title, message: description)
}
}
}
}
}

0 comments on commit edfb0de

Please sign in to comment.