Skip to content

Commit

Permalink
Merge pull request #17 from curityio/feature/include-expires-in
Browse files Browse the repository at this point in the history
Include expiresIn from the tokenResponse in the `TokenResponse` event
  • Loading branch information
daniellindau authored Mar 15, 2024
2 parents d3a58fb + d8e1903 commit 2f1315f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
3 changes: 2 additions & 1 deletion android/src/main/java/io/curity/haapi/react/HaapiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ class HaapiModule(private val _reactContext: ReactApplicationContext) : ReactCon
"accessToken" to tokenResponse.accessToken,
"refreshToken" to tokenResponse.refreshToken,
"scope" to tokenResponse.scope,
"idToken" to tokenResponse.idToken
"idToken" to tokenResponse.idToken,
"expiresIn" to tokenResponse.expiresIn.seconds
)
_tokenResponse = tokenResponse
resolveRequest("TokenResponse", _gson.toJson(tokenMap), promise)
Expand Down
30 changes: 17 additions & 13 deletions ios/HaapiModule/HaapiModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class HaapiModule: RCTEventEmitter {
private func handle(tokenResponse: TokenResponse, promise: Promise) {
switch(tokenResponse) {
case .successfulToken(let successfulTokenResponse):
let tokenResponse = self.mapTokenResponse(successfulTokenResponse)
let tokenResponse = SuccessTokenResponse(successfulTokenResponse)
resolveRequest(eventType: EventType.TokenResponse, body: tokenResponse, promise: promise)
case .errorToken(let errorTokenResponse):
// Request succeeded, but with contents indicating an. Resolve with contents, so that frontend can act on it.
Expand All @@ -247,18 +247,6 @@ class HaapiModule: RCTEventEmitter {
})
}

private func mapTokenResponse(_ succesfulTokenResponse: SuccessfulTokenResponse) -> Dictionary<String, String> {
var tokenResponse = ["accessToken": succesfulTokenResponse.accessToken]
if (succesfulTokenResponse.idToken != nil) {
tokenResponse["idToken"] = succesfulTokenResponse.idToken
}
if (succesfulTokenResponse.refreshToken != nil) {
tokenResponse["refreshToken"] = succesfulTokenResponse.refreshToken
}
tokenResponse["scope"] = succesfulTokenResponse.scope ?? ""
return tokenResponse
}

private func sendHaapiEvent(_ type: EventType, body: Codable, promise: Promise) {
do {
let encodedBody = try encodeObject(body)
Expand Down Expand Up @@ -297,4 +285,20 @@ class HaapiModule: RCTEventEmitter {
}
}

private struct SuccessTokenResponse : Codable {
var accessToken: String
var refreshToken: String?
var idToken: String?
var scope: String
var expiresIn: Int

init(_ tokenResponse : SuccessfulTokenResponse) {
self.accessToken = tokenResponse.accessToken
self.idToken = tokenResponse.idToken
self.refreshToken = tokenResponse.refreshToken
self.scope = tokenResponse.scope ?? ""
self.expiresIn = tokenResponse.expiresIn
}
}

}

0 comments on commit 2f1315f

Please sign in to comment.