Skip to content

Commit

Permalink
Update to latest protos
Browse files Browse the repository at this point in the history
  • Loading branch information
tmarkovski committed Nov 30, 2023
1 parent f8a9b3f commit 7243d7e
Show file tree
Hide file tree
Showing 28 changed files with 3,324 additions and 1,909 deletions.
12 changes: 6 additions & 6 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import Trinsic

struct ContentView: View {
let connect = ConnectClient()

@State private var path = NavigationPath()

var body: some View {
NavigationStack(path: $path) {
VStack {
Button("Verify with Trinsic Connect") {
let request = VerifiablePresentationRequest(ecosystem: "did-hack",
schema: "https://schema.trinsic.cloud/did-hack/attendance-credential")
connect.requestVerifiableCredential(request) { result, error in

connect.requestVerifiableCredential(request) { result, _ in
guard let vp = result else {
return
}
Expand All @@ -36,8 +36,8 @@ struct ContentView: View {
}
}

//struct ContentView_Previews: PreviewProvider {
// struct ContentView_Previews: PreviewProvider {
// static var previews: some View {
// ContentView()
// }
//}
// }
10 changes: 5 additions & 5 deletions Example/Example/JsonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import SwiftUI
struct JsonObjectView: View {
let object: NSDictionary
var body: some View {
let dict = object as! Dictionary<String, Any>
let dict = object as! [String: Any]
let keys = dict.keys.map { $0 }

List {
ForEach(keys, id: \.self) { key in
let value = dict[key]

if let itemValue = value as? String {
VStack(alignment: .leading) {
Text(key)
Expand Down Expand Up @@ -50,9 +50,9 @@ struct JsonArrayView: View {
let object: NSArray
var body: some View {
let items = object as! [Any]

List {
ForEach(0..<items.count, id: \.self) { index in
ForEach(0 ..< items.count, id: \.self) { index in
let item = items[index]
if let textItem = item as? String {
Text(textItem)
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let package = Package(
dependencies: [
.package(name: "grpc-swift", url: "https://github.com/grpc/grpc-swift.git", from: "1.19.0"),
.package(name: "SwiftProtobuf", url: "https://github.com/apple/swift-protobuf.git", from: "1.22.1"),
.package(name: "AppAuth", url: "https://github.com/openid/AppAuth-iOS.git", .upToNextMajor(from: "1.6.2"))
.package(name: "AppAuth", url: "https://github.com/openid/AppAuth-iOS.git", .upToNextMajor(from: "1.6.2")),
],
targets: [
.target(
Expand Down
14 changes: 8 additions & 6 deletions Sources/Trinsic/AccessManagementService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,28 @@ public class AccessManagementService: ServiceBase {
client = Services_Provider_V1_AccessManagementNIOClient(channel: createChannel())
}

// BEGIN Code generated by protoc-gen-trinsic. DO NOT EDIT.
// target: /home/runner/work/sdk-swift/sdk-swift/Sources/Trinsic/AccessManagementService.swift
// BEGIN Code generated by protoc-gen-trinsic. DO NOT EDIT.
// target: /home/runner/work/sdk-swift/sdk-swift/Sources/Trinsic/AccessManagementService.swift

@available(*, deprecated, message: "This method is experimental")
public func addRoleAssignment(request: Services_Provider_V1_AddRoleAssignmentRequest) throws -> Services_Provider_V1_AddRoleAssignmentResponse {
return try client!.AddRoleAssignment(request, callOptions: try buildMetadata(request))
try client!.AddRoleAssignment(request, callOptions: buildMetadata(request))
.response
.wait()
}

@available(*, deprecated, message: "This method is experimental")
public func removeRoleAssignment(request: Services_Provider_V1_RemoveRoleAssignmentRequest) throws -> Services_Provider_V1_RemoveRoleAssignmentResponse {
return try client!.RemoveRoleAssignment(request, callOptions: try buildMetadata(request))
try client!.RemoveRoleAssignment(request, callOptions: buildMetadata(request))
.response
.wait()
}

@available(*, deprecated, message: "This method is experimental")
public func listRoleAssignments(request: Services_Provider_V1_ListRoleAssignmentsRequest) throws -> Services_Provider_V1_ListRoleAssignmentsResponse {
return try client!.ListRoleAssignments(request, callOptions: try buildMetadata(request))
try client!.ListRoleAssignments(request, callOptions: buildMetadata(request))
.response
.wait()
}
// END Code generated by protoc-gen-trinsic. DO NOT EDIT.
// END Code generated by protoc-gen-trinsic. DO NOT EDIT.
}
121 changes: 61 additions & 60 deletions Sources/Trinsic/ConnectClient.swift
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
//
// File.swift
//
// ConnectClient.swift
//
//
// Created by Tomislav Markovski on 8/15/23.
//

import Foundation
import AppAuth
import AuthenticationServices
import Foundation
#if os(iOS)
import UIKit
import UIKit
#elseif os(macOS)
import AppKit
import AppKit
#endif

public class ConnectClient: NSObject, ASWebAuthenticationPresentationContextProviding {
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
public func presentationAnchor(for _: ASWebAuthenticationSession) -> ASPresentationAnchor {
#if os(iOS)
UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
.first { $0.isKeyWindow } ?? ASPresentationAnchor()
UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap(\.windows)
.first { $0.isKeyWindow } ?? ASPresentationAnchor()
#elseif os(macOS)
NSApplication.shared.windows.first { $0.isKeyWindow } ?? ASPresentationAnchor()
NSApplication.shared.windows.first { $0.isKeyWindow } ?? ASPresentationAnchor()
#endif
}
public override init() {}
public func requestVerifiableCredential(_ request: VerifiablePresentationRequest, completion: @escaping (VerifiablePresentation?, ConnectError?) -> Void) -> Void {

override public init() {}

public func requestVerifiableCredential(_ request: VerifiablePresentationRequest, completion: @escaping (VerifiablePresentation?, ConnectError?) -> Void) {
let ecosystem = request.ecosystem.ecosystemName()
let issuer = URL(string: "https://\(ecosystem).connect.trinsic.cloud")!

// discovers endpoints
OIDAuthorizationService.discoverConfiguration(forIssuer: issuer) { configuration, error in
guard let config = configuration else {
print("Error retrieving discovery document: \(error?.localizedDescription ?? "Unknown error")")
return
}

// builds authentication request
let request = OIDAuthorizationRequest(configuration: config,
clientId: "https://example.com",
scopes: [OIDScopeOpenID],
redirectURL: URL(string: "com.example://oauth2redirect")!,
responseType: OIDResponseTypeCode,
additionalParameters: [
"trinsic:ecosystem": ecosystem,
"trinsic:schema": request.schema
"trinsic:ecosystem": ecosystem,
"trinsic:schema": request.schema,
])
let session = ASWebAuthenticationSession(url: request.authorizationRequestURL(), callbackURLScheme: "com.example") { url, error in

let session = ASWebAuthenticationSession(url: request.authorizationRequestURL(), callbackURLScheme: "com.example") { url, _ in
let tokenRequest = OIDTokenRequest(configuration: config,
grantType: OIDGrantTypeAuthorizationCode,
authorizationCode: url?.valueOfQueryParameter(named: "code"),
Expand All @@ -61,42 +61,43 @@ public class ConnectClient: NSObject, ASWebAuthenticationPresentationContextProv
refreshToken: nil,
codeVerifier: request.codeVerifier,
additionalParameters: nil)
OIDAuthorizationService.perform(tokenRequest) { tokenResponse, tokenError in

OIDAuthorizationService.perform(tokenRequest) { tokenResponse, tokenError in
if let error = tokenError {
completion(nil, .networkError(reason: error.localizedDescription))
return
}
guard let response = tokenResponse,
let additionalParams = response.additionalParameters,
let vp = additionalParams["vp_token"] as? NSDictionary else {
let vp = additionalParams["vp_token"] as? NSDictionary
else {
completion(nil, .networkError(reason: "No response"))
return
}

completion(VerifiablePresentation(data: vp), nil)
}
}

session.presentationContextProvider = self
session.start()
}
}

public func requestIdentityVerification (_ completion: @escaping (String, Bool) -> Void) -> Void {
}

public func requestIdentityVerification(_: @escaping (String, Bool) -> Void) {}
}

public class VerifiablePresentation: NSObject {
public init(data: NSDictionary) {
self.data = data
}

public let data: NSDictionary

public var jsonString: String {
if let jsonData = try? JSONSerialization.data(withJSONObject: self.data, options: .withoutEscapingSlashes),
let jsonString = String(data: jsonData, encoding: .utf8) {
if let jsonData = try? JSONSerialization.data(withJSONObject: data, options: .withoutEscapingSlashes),
let jsonString = String(data: jsonData, encoding: .utf8)
{
return jsonString
} else {
return "Error converting NSDictionary to JSON string"
Expand All @@ -108,19 +109,19 @@ public struct VerifiablePresentationRequest: Codable {
public init(ecosystem: String, schema: String) {
self.ecosystem = ecosystem
self.schema = schema
self.issuer = nil
self.challenge = nil
self.domain = nil
issuer = nil
challenge = nil
domain = nil
}

public init(ecosystem: String, schema: String, issuer: String?, challenge: String?, domain: String?) {
self.ecosystem = ecosystem
self.schema = schema
self.issuer = issuer
self.challenge = challenge
self.domain = domain
}

public var schema: String
public var ecosystem: String
public var issuer: String?
Expand All @@ -134,26 +135,26 @@ public enum ConnectError: Swift.Error {
}

#if os(iOS)
extension UIApplication {
public var topViewController: UIViewController? {
var topViewController: UIViewController? = nil
if #available(iOS 13, *) {
topViewController = connectedScenes.compactMap {
return ($0 as? UIWindowScene)?.windows.filter { $0.isKeyWindow }.first?.rootViewController
}.first
} else {
topViewController = keyWindow?.rootViewController
}
if let presented = topViewController?.presentedViewController {
topViewController = presented
} else if let navController = topViewController as? UINavigationController {
topViewController = navController.topViewController
} else if let tabBarController = topViewController as? UITabBarController {
topViewController = tabBarController.selectedViewController
public extension UIApplication {
var topViewController: UIViewController? {
var topViewController: UIViewController?
if #available(iOS 13, *) {
topViewController = connectedScenes.compactMap {
($0 as? UIWindowScene)?.windows.filter(\.isKeyWindow).first?.rootViewController
}.first
} else {
topViewController = keyWindow?.rootViewController
}
if let presented = topViewController?.presentedViewController {
topViewController = presented
} else if let navController = topViewController as? UINavigationController {
topViewController = navController.topViewController
} else if let tabBarController = topViewController as? UITabBarController {
topViewController = tabBarController.selectedViewController
}
return topViewController
}
return topViewController
}
}
#endif

extension URL {
Expand All @@ -165,11 +166,11 @@ extension URL {

extension String {
func ecosystemName() -> String {
if self.hasPrefix("urn:trinsic:ecosystems:") {
return String(self.dropFirst("urn:trinsic:ecosystems:".count))
if hasPrefix("urn:trinsic:ecosystems:") {
return String(dropFirst("urn:trinsic:ecosystems:".count))
}
if self.hasPrefix("urn:ecosystems:") {
return String(self.dropFirst("urn:ecosystems:".count))
if hasPrefix("urn:ecosystems:") {
return String(dropFirst("urn:ecosystems:".count))
}
return self
}
Expand Down
Loading

0 comments on commit 7243d7e

Please sign in to comment.