-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Metadata Network Connection #306
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
68288d9
add a network connection data provider. create a new folder for metad…
tobitech 039d959
update network metadata item when there is a change in the connection…
tobitech 1eb4f68
use combine publisher instead of notification center.
tobitech ebd781b
code formatting.
tobitech 5818f58
formating.
tobitech 4b10760
Merge branch 'main' into metada-network-connection
tobitech 995a116
provide support for any encodable type for metadata value. use a sing…
tobitech 3196fc4
replace localmetadata with metadata manager in different parts of the…
tobitech 392343f
data and code formatting.
tobitech 330606e
use anycodable to ensure only codable values are used for metadata.
tobitech 4722f0e
import a new implementation for AnyCodable.
tobitech 1b09f1d
Merge branch 'main' into metada-network-connection
tobitech 40c0167
move helper class into helpers group.re move import check.
tobitech 8697d78
code formatting.
tobitech 5f21070
Merge branch 'fraud-signals-metadata' into metada-network-connection
tobitech ea01483
use string type for metadatum value.
tobitech 2ea8d78
fix missing import
tobitech 5fce76d
Remove codable helpers
tobitech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Sources/SmileID/Classes/Helpers/NetworkMetadataProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Foundation | ||
import Network | ||
import Combine | ||
|
||
/// A class for determining the current network connection type (Wi-Fi, cellular, VPN, etc.) | ||
class NetworkMetadataProvider { | ||
private let monitor: NWPathMonitor | ||
/// Array tracking connection types over time. | ||
private var connectionTypes: [String] = [] | ||
|
||
init() { | ||
monitor = NWPathMonitor() | ||
monitor.pathUpdateHandler = { [weak self] path in | ||
guard let self else { return } | ||
let newConnection = if path.usesInterfaceType(.wifi) { | ||
"wifi" | ||
} else if path.usesInterfaceType(.cellular) { | ||
"cellular" | ||
} else { | ||
"other" | ||
} | ||
|
||
if self.connectionTypes.last != newConnection { | ||
self.connectionTypes.append(newConnection) | ||
} | ||
} | ||
|
||
let queue = DispatchQueue(label: "NetworkMonitor") | ||
monitor.start(queue: queue) | ||
} | ||
|
||
deinit { | ||
monitor.cancel() | ||
} | ||
} | ||
|
||
extension NetworkMetadataProvider: MetadataProvider { | ||
func collectMetadata() -> [MetadataKey: String] { | ||
if let jsonData = try? JSONSerialization.data(withJSONObject: connectionTypes, options: []), | ||
let jsonString = String(data: jsonData, encoding: .utf8) { | ||
return [.networkConnection: jsonString] | ||
} | ||
return [.networkConnection: "unknown"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Foundation | ||
|
||
extension TimeInterval { | ||
func milliseconds() -> String { | ||
return String(Int(self * 1000)) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
Sources/SmileID/Classes/Networking/Metadata/IPAddressProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Foundation | ||
|
||
func getIPAddress(useIPv4: Bool) -> String { | ||
var address = "" | ||
var ifaddr: UnsafeMutablePointer<ifaddrs>? | ||
|
||
guard getifaddrs(&ifaddr) == 0 else { | ||
return "" | ||
} | ||
|
||
var ptr = ifaddr | ||
while ptr != nil { | ||
defer { ptr = ptr?.pointee.ifa_next } | ||
|
||
guard let interface = ptr?.pointee else { | ||
return "" | ||
} | ||
|
||
let addrFamily = interface.ifa_addr.pointee.sa_family | ||
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) { | ||
let name = String(cString: interface.ifa_name) | ||
if name == "en0" || name == "en1" || name == "pdp_ip0" | ||
|| name == "pdp_ip1" || name == "pdp_ip2" || name == "pdp_ip3" { | ||
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST)) | ||
getnameinfo( | ||
interface.ifa_addr, | ||
socklen_t(interface.ifa_addr.pointee.sa_len), | ||
&hostname, socklen_t(hostname.count), | ||
nil, socklen_t(0), NI_NUMERICHOST) | ||
address = String(cString: hostname) | ||
|
||
if (useIPv4 && addrFamily == UInt8(AF_INET)) | ||
|| (!useIPv4 && addrFamily == UInt8(AF_INET6)) { | ||
if !useIPv4 { | ||
if let percentIndex = address.firstIndex(of: "%") { | ||
address = String(address[..<percentIndex]) | ||
.uppercased() | ||
} else { | ||
address = address.uppercased() | ||
} | ||
} | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
freeifaddrs(ifaddr) | ||
return address | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
potential difference with android smileidentity/android#580 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be the same behaviour and it gets json serialised into as string here -
ios/Sources/SmileID/Classes/Helpers/NetworkMetadataProvider.swift
Line 39 in 5fce76d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The backend API expects the metadata values be converted into a json string no matter the type of data we are collecting.