-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
133 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// AuthenticationEvent.swift | ||
// | ||
// | ||
// Created by Terry Yiu on 5/1/24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// An event that provides a way for clients to authenticate to relays. | ||
/// This kind is not meant to be published or queried. | ||
/// | ||
/// See [NIP-42](https://github.com/nostr-protocol/nips/blob/master/42.md). | ||
public final class AuthenticationEvent: NostrEvent, RelayProviding, RelayURLValidating { | ||
public required init(from decoder: Decoder) throws { | ||
try super.init(from: decoder) | ||
} | ||
|
||
@available(*, unavailable, message: "This initializer is unavailable for this class.") | ||
override init(kind: EventKind, content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws { | ||
try super.init(kind: kind, content: content, tags: tags, createdAt: createdAt, signedBy: keypair) | ||
} | ||
|
||
init(content: String, tags: [Tag] = [], createdAt: Int64 = Int64(Date.now.timeIntervalSince1970), signedBy keypair: Keypair) throws { | ||
try super.init(kind: .authentication, content: content, tags: tags, createdAt: createdAt, signedBy: keypair) | ||
} | ||
|
||
/// The relay URL where this event authenticates to it. | ||
public var relayURL: URL? { | ||
guard let relayURLString = firstValueForRawTagName("relay") else { | ||
return nil | ||
} | ||
|
||
return try? validateRelayURLString(relayURLString) | ||
} | ||
|
||
/// The challenge string as received from the relay. | ||
public var challenge: String? { | ||
firstValueForRawTagName("challenge") | ||
} | ||
} | ||
|
||
public extension EventCreating { | ||
|
||
func authenticate(relayURL: URL, challenge: String, signedBy keypair: Keypair) throws -> AuthenticationEvent { | ||
let validatedRelayURL = try RelayURLValidator.shared.validateRelayURL(relayURL) | ||
|
||
let tags: [Tag] = [ | ||
Tag(name: "relay", value: validatedRelayURL.absoluteString), | ||
Tag(name: "challenge", value: challenge) | ||
] | ||
|
||
return try AuthenticationEvent(content: "", tags: tags, signedBy: keypair) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// AuthenticationEventTests.swift | ||
// | ||
// | ||
// Created by Terry Yiu on 5/2/24. | ||
// | ||
|
||
@testable import NostrSDK | ||
import XCTest | ||
|
||
final class AuthenticationEventTests: XCTestCase, EventCreating, EventVerifying, FixtureLoading { | ||
|
||
func testCreateAuthenticationEvent() throws { | ||
let relayURL = try XCTUnwrap(URL(string: "wss://relay.example.com/")) | ||
let event = try authenticate(relayURL: relayURL, challenge: "some-challenge-string", signedBy: Keypair.test) | ||
|
||
XCTAssertEqual(event.kind, .authentication) | ||
XCTAssertEqual(event.relayURL, relayURL) | ||
XCTAssertEqual(event.challenge, "some-challenge-string") | ||
|
||
try verifyEvent(event) | ||
} | ||
|
||
func testDecodeAuthenticationEvent() throws { | ||
let event: AuthenticationEvent = try decodeFixture(filename: "authentication_event") | ||
|
||
XCTAssertEqual(event.id, "adb599bc2f6b4cf97d927de2cb36829326c86013e0a6e8f51159f80938a5c246") | ||
XCTAssertEqual(event.pubkey, "9947f9659dd80c3682402b612f5447e28249997fb3709500c32a585eb0977340") | ||
XCTAssertEqual(event.createdAt, 1714625219) | ||
XCTAssertEqual(event.kind, .authentication) | ||
|
||
let expectedTags: [Tag] = [ | ||
Tag(name: "relay", value: "wss://relay.example.com/"), | ||
Tag(name: "challenge", value: "some-challenge-string") | ||
] | ||
XCTAssertEqual(event.tags, expectedTags) | ||
XCTAssertEqual(event.content, "") | ||
XCTAssertEqual(event.signature, "b27181e0b72872c463ac75ebf3ad2c2502696d81551bbae6b2a391c67614daf2e321eb5ab724b04520b26fcf7a4c9823fefdb47b10d66c088db44162ba9c1291") | ||
} | ||
|
||
} |
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 @@ | ||
["AUTH",{"id":"adb599bc2f6b4cf97d927de2cb36829326c86013e0a6e8f51159f80938a5c246","pubkey":"9947f9659dd80c3682402b612f5447e28249997fb3709500c32a585eb0977340","created_at":1714625219,"kind":22242,"tags":[["relay","wss://relay.example.com/"],["challenge","some-challenge-string"]],"content":"","sig":"b27181e0b72872c463ac75ebf3ad2c2502696d81551bbae6b2a391c67614daf2e321eb5ab724b04520b26fcf7a4c9823fefdb47b10d66c088db44162ba9c1291"}] |
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,12 @@ | ||
{ | ||
"id": "adb599bc2f6b4cf97d927de2cb36829326c86013e0a6e8f51159f80938a5c246", | ||
"pubkey": "9947f9659dd80c3682402b612f5447e28249997fb3709500c32a585eb0977340", | ||
"created_at": 1714625219, | ||
"kind": 22242, | ||
"tags": [ | ||
["relay", "wss://relay.example.com/"], | ||
["challenge", "some-challenge-string"] | ||
], | ||
"content": "", | ||
"sig": "b27181e0b72872c463ac75ebf3ad2c2502696d81551bbae6b2a391c67614daf2e321eb5ab724b04520b26fcf7a4c9823fefdb47b10d66c088db44162ba9c1291" | ||
} |
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