Skip to content

Commit

Permalink
Fixed and added GA tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldavidw committed Jul 25, 2024
1 parent b7cfbb1 commit 466e603
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Chronos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
6BC5F04A2C4FDE6E00BA106F /* ParseOtpAuthUrl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BC5F0492C4FDE6E00BA106F /* ParseOtpAuthUrl.swift */; };
6BC5F0522C52429100BA106F /* SwiftProtobuf in Frameworks */ = {isa = PBXBuildFile; productRef = 6BC5F0512C52429100BA106F /* SwiftProtobuf */; };
6BC5F0552C52464600BA106F /* GoogleAuth.pb.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BC5F0542C5242B500BA106F /* GoogleAuth.pb.swift */; };
6BC5F0572C529A2A00BA106F /* GoogleAuthenticator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BC5F0562C529A2A00BA106F /* GoogleAuthenticator.swift */; };
6BD6D2012C11FEB4004512BF /* OTPService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BD6D2002C11FEB4004512BF /* OTPService.swift */; };
6BD90AA52B8E34BB00FABD91 /* PasswordLoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BD90AA42B8E34BB00FABD91 /* PasswordLoginView.swift */; };
6BE122922BD6413D008636D2 /* ChronosCrypto.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BE122912BD6413D008636D2 /* ChronosCrypto.swift */; };
Expand Down Expand Up @@ -142,6 +143,7 @@
6BC3C3B42BA6B91E00B181B9 /* BiometricsSetupView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BiometricsSetupView.swift; sourceTree = "<group>"; };
6BC5F0492C4FDE6E00BA106F /* ParseOtpAuthUrl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParseOtpAuthUrl.swift; sourceTree = "<group>"; };
6BC5F0542C5242B500BA106F /* GoogleAuth.pb.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleAuth.pb.swift; sourceTree = "<group>"; };
6BC5F0562C529A2A00BA106F /* GoogleAuthenticator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleAuthenticator.swift; sourceTree = "<group>"; };
6BD6D2002C11FEB4004512BF /* OTPService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OTPService.swift; sourceTree = "<group>"; };
6BD90AA42B8E34BB00FABD91 /* PasswordLoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PasswordLoginView.swift; sourceTree = "<group>"; };
6BE122912BD6413D008636D2 /* ChronosCrypto.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChronosCrypto.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -316,6 +318,7 @@
children = (
6B4CBF2E2C490FB700983D44 /* Chronos.swift */,
6B8132F12C4975DA00DB367E /* Raivo.swift */,
6BC5F0562C529A2A00BA106F /* GoogleAuthenticator.swift */,
);
path = Import;
sourceTree = "<group>";
Expand Down Expand Up @@ -681,6 +684,7 @@
files = (
6B8132FD2C4E5F6B00DB367E /* QrCodeGenerationAndParsing.swift in Sources */,
6B4CBF2F2C490FB700983D44 /* Chronos.swift in Sources */,
6BC5F0572C529A2A00BA106F /* GoogleAuthenticator.swift in Sources */,
6BC5F04A2C4FDE6E00BA106F /* ParseOtpAuthUrl.swift in Sources */,
6B8132F22C4975DA00DB367E /* Raivo.swift in Sources */,
6B8132FA2C4C0F6300DB367E /* TokenToOtpAuthUrl.swift in Sources */,
Expand Down
18 changes: 12 additions & 6 deletions Chronos/Services/Import/ImportService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extension ImportService {
}
}

private func importFromRaivo(json: JSON) -> [Token]? {
func importFromRaivo(json: JSON) -> [Token]? {
var tokens: [Token] = []

for (key, subJson) in json {
Expand Down Expand Up @@ -105,7 +105,7 @@ extension ImportService {
return tokens
}

private func importFromGoogleAuth(otpAuthMigration: String) -> [Token]? {
func importFromGoogleAuth(otpAuthMigration: String) -> [Token]? {
guard let otpAuthMigrationUrl = URL(string: otpAuthMigration),
let components = URLComponents(url: otpAuthMigrationUrl, resolvingAgainstBaseURL: false),
let scheme = components.scheme, scheme == "otpauth-migration",
Expand Down Expand Up @@ -155,17 +155,23 @@ extension ImportService {
default:
tokenAlgo = TokenAlgorithmEnum.SHA1
}

let token = Token()
token.issuer = gaToken.issuer
token.account = gaToken.name
token.digits = tokenDigits
token.period = 30
token.counter = Int(gaToken.counter)
token.type = tokenType
token.algorithm = tokenAlgo
token.secret = gaToken.secret.base32EncodedString


if tokenType == .TOTP {
token.period = 30 // GA only allows 30 secs
}

if tokenType == .HOTP {
token.counter = Int(gaToken.counter)
}

tokens.append(token)
}

Expand Down
59 changes: 59 additions & 0 deletions ChronosTests/Import/GoogleAuthenticator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
@testable import Chronos
import SwiftyJSON
import XCTest

final class GoogleAuthenticatorTests: XCTestCase {
func testValidImport() throws {
let authOtpMigratation = "otpauth-migration://offline?data=Ci0KCkhlbGxvId6tvu8SEmpvaG5AYXBwbGVzZWVkLmNvbRoFQXBwbGUgASgBMAIKLgoKSGVsbG8h3q2%2B7xITam9objJAYXBwbGVzZWVkLmNvbRoFQXBwbGUgAigCMAIKNAoKSGVsbG8h3q2%2B7xIXam9obitob3RwQGFwcGxlc2VlZC5jb20aBUFwcGxlIAEoATABOAAKNQoKSGVsbG8h3q2%2B7xIYam9obitob3RwMkBhcHBsZXNlZWQuY29tGgVBcHBsZSABKAIwATgAEAIYASAA"

let importService = ImportService()
let tokens = importService.importFromGoogleAuth(otpAuthMigration: authOtpMigratation)!

XCTAssertEqual(tokens.count, 4)

XCTAssertEqual(tokens[0].digits, 6)
XCTAssertEqual(tokens[0].type, TokenTypeEnum.TOTP)
XCTAssertEqual(tokens[0].counter, 0)
XCTAssertEqual(tokens[0].algorithm, TokenAlgorithmEnum.SHA1)
XCTAssertEqual(tokens[0].issuer, "Apple")
XCTAssertEqual(tokens[0].account, "[email protected]")
XCTAssertEqual(tokens[0].period, 30)
XCTAssertEqual(tokens[0].secret, "JBSWY3DPEHPK3PXP")

XCTAssertEqual(tokens[1].digits, 8)
XCTAssertEqual(tokens[1].type, TokenTypeEnum.TOTP)
XCTAssertEqual(tokens[1].counter, 0)
XCTAssertEqual(tokens[1].algorithm, TokenAlgorithmEnum.SHA256)
XCTAssertEqual(tokens[1].issuer, "Apple")
XCTAssertEqual(tokens[1].account, "[email protected]")
XCTAssertEqual(tokens[1].period, 30)
XCTAssertEqual(tokens[1].secret, "JBSWY3DPEHPK3PXP")

XCTAssertEqual(tokens[2].digits, 6)
XCTAssertEqual(tokens[2].type, TokenTypeEnum.HOTP)
XCTAssertEqual(tokens[2].counter, 0)
XCTAssertEqual(tokens[2].algorithm, TokenAlgorithmEnum.SHA1)
XCTAssertEqual(tokens[2].issuer, "Apple")
XCTAssertEqual(tokens[2].account, "[email protected]")
XCTAssertEqual(tokens[2].counter, 0)
XCTAssertEqual(tokens[2].secret, "JBSWY3DPEHPK3PXP")

XCTAssertEqual(tokens[3].digits, 8)
XCTAssertEqual(tokens[3].type, TokenTypeEnum.HOTP)
XCTAssertEqual(tokens[3].counter, 0)
XCTAssertEqual(tokens[3].algorithm, TokenAlgorithmEnum.SHA1)
XCTAssertEqual(tokens[3].issuer, "Apple")
XCTAssertEqual(tokens[3].account, "[email protected]")
XCTAssertEqual(tokens[3].counter, 0)
XCTAssertEqual(tokens[3].secret, "JBSWY3DPEHPK3PXP")
}

func testInvalidImport_AlgoMD5() throws {
let authOtpMigratation = "otpauth-migration://offline?data=Ci0KCkhlbGxvId6tvu8SEmpvaG5AYXBwbGVzZWVkLmNvbRoFQXBwbGUgASgBMAIKLgoKSGVsbG8h3q2%2B6RIRbWQ1QGFwcGxlc2VlZC5jb20aBUFwcGxlIAQoATABOAAQAhgBIAA%3D"

let importService = ImportService()
let tokens = importService.importFromGoogleAuth(otpAuthMigration: authOtpMigratation)

XCTAssertNil(tokens, "Should fail fast if any tokens contains md5 algo")
}
}

0 comments on commit 466e603

Please sign in to comment.