Skip to content

Commit

Permalink
Merge pull request #256 from stytchauth/nfakhouri-sdk-1726
Browse files Browse the repository at this point in the history
[SDK-1726]: Require non-null session_token and session_jwt in updateSession
  • Loading branch information
nidal-stytch authored Jun 28, 2024
2 parents ada6316 + f1b4f85 commit 475bb04
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
20 changes: 20 additions & 0 deletions Sources/StytchCore/StytchClientCommon/Models/SessionToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,23 @@ public struct SessionToken: Equatable {
.init(kind: .opaque, value: value)
}
}

/// A public interface to require the caller to explicitly pass one of each type of non nil token in order to update a session.
public struct SessionTokens {
let jwt: SessionToken
let opaque: SessionToken

init?(jwt: SessionToken, opaque: SessionToken) {
if jwt.kind != .jwt, opaque.kind != .opaque {
return nil
}

self.jwt = jwt
self.opaque = opaque
}

func updatePersistentStorage(sessionStorage: SessionStorage) {
sessionStorage.updatePersistentStorage(token: jwt)
sessionStorage.updatePersistentStorage(token: opaque)
}
}
6 changes: 3 additions & 3 deletions Sources/StytchCore/StytchClientCommon/Sessions/Sessions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public struct Sessions<AuthResponseType: Decodable> {

/// If your app has cookies disabled or simply receives updated session tokens from your backend via means other than
/// `Set-Cookie` headers, you must call this method after receiving the updated tokens to ensure the `StytchClient`
/// and persistent storage are kept up-to-date. You should include both the opaque token and the jwt.
public func update(sessionTokens tokens: [SessionToken]) {
tokens.forEach(sessionStorage.updatePersistentStorage)
/// and persistent storage are kept up-to-date. You are required to include both the opaque token and the jwt.
public func update(sessionTokens: SessionTokens) {
sessionTokens.updatePersistentStorage(sessionStorage: sessionStorage)
}

// sourcery: AsyncVariants, (NOTE: - must use /// doc comment styling)
Expand Down
11 changes: 7 additions & 4 deletions Tests/StytchCoreTests/B2BSessionsTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ final class B2BSessionsTestCase: BaseTestCase {
XCTAssertNil(StytchB2BClient.sessions.sessionToken)
XCTAssertNil(StytchB2BClient.sessions.sessionJwt)

StytchB2BClient.sessions.update(sessionTokens: [.opaque("token"), .jwt("jwt")])

XCTAssertEqual(StytchB2BClient.sessions.sessionToken, .opaque("token"))
XCTAssertEqual(StytchB2BClient.sessions.sessionJwt, .jwt("jwt"))
if let tokens = SessionTokens(jwt: .jwt("jwt"), opaque: .opaque("token")) {
StytchB2BClient.sessions.update(sessionTokens: tokens)
XCTAssertEqual(StytchB2BClient.sessions.sessionToken, .opaque("token"))
XCTAssertEqual(StytchB2BClient.sessions.sessionJwt, .jwt("jwt"))
} else {
XCTFail("SessionTokens should not be nil")
}
}
}
11 changes: 7 additions & 4 deletions Tests/StytchCoreTests/SessionsTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ final class SessionsTestCase: BaseTestCase {
XCTAssertNil(StytchClient.sessions.sessionToken)
XCTAssertNil(StytchClient.sessions.sessionJwt)

StytchClient.sessions.update(sessionTokens: [.opaque("token"), .jwt("jwt")])

XCTAssertEqual(StytchClient.sessions.sessionToken, .opaque("token"))
XCTAssertEqual(StytchClient.sessions.sessionJwt, .jwt("jwt"))
if let tokens = SessionTokens(jwt: .jwt("jwt"), opaque: .opaque("token")) {
StytchClient.sessions.update(sessionTokens: tokens)
XCTAssertEqual(StytchClient.sessions.sessionToken, .opaque("token"))
XCTAssertEqual(StytchClient.sessions.sessionJwt, .jwt("jwt"))
} else {
XCTFail("SessionTokens should not be nil")
}
}
}

0 comments on commit 475bb04

Please sign in to comment.