Skip to content

Commit

Permalink
Removes hmacEnabled property on MagicbellClient, in favour of just …
Browse files Browse the repository at this point in the history
…using `hmac` property on `connectUser`
  • Loading branch information
stigi committed Apr 11, 2024
1 parent 6bae840 commit ba8e20d
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 15 deletions.
1 change: 0 additions & 1 deletion Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ extension MagicBellClient {
/// Application global instance of MagicBellClient
static var shared = MagicBellClient(
apiKey: "34ed17a8482e44c765d9e163015a8d586f0b3383",
enableHMAC: true,
logLevel: .debug
)
}
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ You can provide additional options when initializing a client:
```swift
let magicbell = MagicBellClient(
apiKey: "[MAGICBELL_API_KEY]"
enableHMAC: true,
logLevel: .debug
)
```
Expand All @@ -125,7 +124,6 @@ let magicbell = MagicBellClient(
| ------------ | ------------- | -------------------------------------------------------------------------------------------- |
| `apiKey` | - | Your MagicBell's API key |
| `apiSecret` | `nil` | Your MagicBell's API secret |
| `enableHMAC` | `false` | Set it to `true` if you want HMAC enabled. Note the `apiSecret` is required if set to `true` |
| `logLevel` | `.none` | Set it to `.debug` to enable logs |

Though the API key is meant to be published, you should not distribute the API secret. Rather, enable HMAC for your
Expand Down Expand Up @@ -169,7 +167,7 @@ let user = magicbell.connectUser(externalId: "001")
let user = magicbell.connectUser(email: "[email protected]", externalId: "001")
```

Each variant of `connectUser` supports an optional `hmac` parameter that is required if `enableHMAC` is enabled when initiating the `MagicBellClient`.
Each variant of `connectUser` supports an optional `hmac` parameter that should be send when HMAC Security was enabled for the project.

You can connect as [many users as you need](#multi-user-support).

Expand Down
1 change: 0 additions & 1 deletion Source/Common/Environment/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ import Foundation
struct Environment {
let apiKey: String
let baseUrl: URL
let isHMACEnabled: Bool
}
3 changes: 1 addition & 2 deletions Source/Common/Network/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ class DefaultHttpClient: HttpClient {

urlRequest.addValue(environment.apiKey, forHTTPHeaderField: "X-MAGICBELL-API-KEY")

if environment.isHMACEnabled,
let hmac = hmac {
if let hmac = hmac {
urlRequest.addValue(hmac, forHTTPHeaderField: "X-MAGICBELL-USER-HMAC")
}
addIdAndOrEmailHeader(externalId, email, &urlRequest)
Expand Down
4 changes: 1 addition & 3 deletions Source/Features/StoreRealTime/AblyConnector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ class AblyConnector: StoreRealTime {
options.authMethod = "POST"
let headers = self.generateAblyHeaders(
apiKey: self.environment.apiKey,
isHMACEnabled: self.environment.isHMACEnabled,
externalId: self.userQuery.externalId,
email: self.userQuery.email,
hmac: self.userQuery.hmac
Expand All @@ -97,14 +96,13 @@ class AblyConnector: StoreRealTime {

private func generateAblyHeaders(
apiKey: String,
isHMACEnabled: Bool,
externalId: String?,
email: String?,
hmac: String?
) -> [String: String] {

var headers = ["X-MAGICBELL-API-KEY": apiKey]
if isHMACEnabled, let hmac = hmac {
if let hmac = hmac {
headers["X-MAGICBELL-USER-HMAC"] = hmac
}

Expand Down
6 changes: 1 addition & 5 deletions Source/MagicBellClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,18 @@ public class MagicBellClient {
/// - Parameters:
/// - apiKey: The API Key of your MagicBell project.
/// - apiSecret: The API secret of your MagicBell project. Defaults to `nil`.
/// - enableHMAC: Use HMAC authentication. Defaults to `false`. If set to `true`, HMAC will be only enabled if the
/// API secret is set.
/// - baseUrl: URL of the API server. Defaults to `MagicBellClient.defaultBaseUrl`.
/// - logLevel: The log level, it accepts `.none` or `.debug`. Defaults to `.none`.
public init(
apiKey: String,
enableHMAC: Bool = false,
// swiftlint:disable force_unwrapping
baseUrl: URL = URL(string: "https://api.magicbell.com")!,
logLevel: LogLevel = .none
) {
sdkProvider = DefaultSDKModule(
environment: Environment(
apiKey: apiKey,
baseUrl: baseUrl,
isHMACEnabled: enableHMAC
baseUrl: baseUrl
),
logLevel: logLevel
)
Expand Down

0 comments on commit ba8e20d

Please sign in to comment.