Skip to content

Commit

Permalink
Refactor JWT utils to remove duplication
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Pietrek <tomasz@nats.io>
  • Loading branch information
Jarema committed Feb 1, 2024
1 parent 74ab8fe commit 7a5c204
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions Sources/NatsSwift/NatsJwtUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ class JwtUtils {
guard let contentsString = String(data: contents, encoding: .utf8) else {
return nil
}
let matches = userConfigRE.matches(in: contentsString, options: [], range: NSRange(contentsString.startIndex..., in: contentsString))
if let match = matches.first, let range = Range(match.range(at: 1), in: contentsString) {
return String(contentsString[range]).data(using: .utf8)
if let match = parseDecoratedJWT(contents: contentsString) {
return match.data(using: .utf8)
}
return nil
}
Expand All @@ -38,15 +37,14 @@ class JwtUtils {
}
return nil
}

/// Parses a credentials file and returns its nkey.
static func parseDecoratedNKey(contents: Data) -> Data? {
guard let contentsString = String(data: contents, encoding: .utf8) else {
return nil
}
let matches = userConfigRE.matches(in: contentsString, options: [], range: NSRange(contentsString.startIndex..., in: contentsString))
if matches.count > 1, let range = Range(matches[1].range(at: 1), in: contentsString) {
return String(contentsString[range]).data(using: .utf8)
if let match = parseDecoratedNKey(contents: contentsString) {
return match.data(using: .utf8)
}
return nil
}
Expand Down

0 comments on commit 7a5c204

Please sign in to comment.