RxMoyaAuthenticatable 0.2.0

RxMoyaAuthenticatable 0.2.0

Maintained by Keita Oouchi.



  • By
  • keita.oouchi

RxMoyaAuthenticatable

Carthage compatible Version License Platform

Make your API token refreshable with RxMoyaAuthenticatable!

RxMoyaAuthenticatable provides standard requestClosure which refresh accessToken automatically on your needs!

This requestClosure intercept your requests, execute followings:

  • suspend all requests in a serial dispatch queue,
  • find persisted authentication,
  • refresh found authentication if needed,
  • resume other requests in a serial queue.

How to use

Adapt RefreshableAuthentication protocol to your Authentication entity:

public protocol RefreshableAuthentication {
  var isRefreshNeeded: Bool { get }
  var accessToken: String { get }
  func refresh() -> Single<Self>

  static func find() -> Self?
}

For example:

import RxSwift
import Moya
import RxMoyaAuthenticatable

struct SpotifyAuthentication: RefreshableAuthentication, Decodable {

  let accessToken: String
  var refreshToken: String!
  var createdAt: Date = Date()
  let expiresIn: Int

  var isRefreshNeeded: Bool {
    let threshold = TimeInterval(self.expiresIn / 2)
    let result = self.expiresAt < Date().addingTimeInterval(threshold)
    return result
  }

  static func find() -> SpotifyAuthentication? {
    return SpotifyAuthenticationStore.find()
  }

  func refresh() -> Single<SpotifyAuthentication> {
    let refreshToken = self.refreshToken
    return
      SpotifyAuthAPI
        .sharedProvider
        .rx
        .request(.refreshToken(refreshToken: self.refreshToken))
        .map { response -> SpotifyAuthentication in
          if var authentication = try? JSONDecoder().decode(SpotifyAuthentication.self, from: response.data) {
            authentication.refreshToken = refreshToken
            return authentication
          } else {
            throw SpotifyAPIError.mappingError
          }
        }.do(
          onSuccess: { authentication in
            SpotifyAuthenticationStore.save(authentication: authentication)
          },
          onError: { error in
            print(error)
          }
        )
  }
}

extension SpotifyAuthentication {

  enum CodingKeys: String, CodingKey {
    case accessToken = "access_token"
    case refreshToken = "refresh_token"
    case expiresIn = "expires_in"
  }

  var expiresAt: Date {
    return createdAt.addingTimeInterval(TimeInterval(expiresIn))
  }
}

(See example implementation using Spotify's API here)

Using this and your API, accessToken will automatically refresh when isRefreshNeeded returns True:

let provider = MoyaProvider<SpotifyAPI>(
  requestClosure: RxMoyaAuthenticatable<SpotifyAPI, SpotifyAuthentication>().requestClosure
)

Example

To run the example project, clone the repo, and see Example/Readme.md. In this example, we implemented Spotify's Authorization Code Flow.

Requirements

Target Version
iOS => 11.0
Swift => 4.0
Moya/RxSwift => 11.0

Installation

RxMoyaAuthenticatable is available through CocoaPods or Carthage.

CocoaPods

pod 'RxMoyaAuthenticatable'

Carthage

github "Moya/Moya"
github "keitaoouchi/RxMoyaAuthenticatable"

Author

keitaoouchi, [email protected]

License

RxMoyaAuthenticatable is available under the MIT license. See the LICENSE file for more info.