【问题标题】:Using Moya for iOS to obtain an access token from a server使用 Moya for iOS 从服务器获取访问令牌
【发布时间】:2021-05-05 01:23:35
【问题描述】:

我将Moya 用于iOS 网络。 我们的服务器(在虚拟机上)使用 OAuth,并且需要使用访问令牌发出请求。 我正在尝试使用POST /oauth/token 将初始访问令牌检索到我们的端点。

我得到的是:

此服务器的证书无效。您可能正在连接到伪装成“xxxxxx”的服务器,这可能会使您的机密信息面临风险。”您仍然要连接到服务器吗?

VM 不验证证书。

这可能是问题吗?

如果是这样,应该如何配置 Moya 以忽略证书验证?

如果没有,关于如何使用 Moya 来实现此目的的任何想法?

下面是Moya配置:

enum MyApi {
    case auth
}

extension MyApi: TargetType {
    var baseURL: URL {
        return URL(string: "https://server.nb/api/oauth/token")!
    }
    
    var path: String {
        return ""
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var sampleData: Data {
        return Data()
    }
    
    var task: Task {
        let params = [
            "client_id": "xxxxxxx",
            "client_secret": "xxxxxxxx",
            "grant_type": "password",
            "password": "xxxxxxxx",
            "username": "niv@bp.com"
        ]
        
        return .requestParameters(parameters: params, encoding: URLEncoding.default)
    }
    
    var headers: [String : String]? {
        let headers = [
            "Content-Type": " application/x-www-form-urlencoded",
        ]
        return headers
    }
}

编辑:

这是 info.plist:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>https://server.nb</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <false/>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSAllowsArbitraryLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>

这是 Moya 提供者服务:

struct MoyaNetworkService {
    private let moyaProvider: MoyaProvider<MyApi>!

    init(moyaProvider: MoyaProvider<MyApi> = MoyaProvider<MyApi>()) {
        self.moyaProvider = moyaProvider
    }

    func auth() {
        moyaProvider.request(.auth) { (result) in
        }
    }
}

【问题讨论】:

标签: ios swift ssl access-token moya


【解决方案1】:
extension ServerTrustPolicy {

    static let defaultTrustPolicy: ServerTrustPolicy = .pinPublicKeys(
        publicKeys: ServerTrustPolicy.publicKeys(),
        validateCertificateChain: true,
        validateHost: true
    )
    
    static let noEvaluationPolicy: ServerTrustPolicy = .disableEvaluation <== this is what you need
}

fileprivate lazy var trustPolicies: [String: ServerTrustPolicy] = {
        var trustPolicies = [String :ServerTrustPolicy]()
        trustPolicies["your_host_sans_https"] = .noEvaluationPolicy
        return trustPolicies
}()

fileprivate let manager = Manager (
    configuration: alamofireConfiguration,
    serverTrustPolicyManager: ServerTrustPolicyManager(policies: trustPolicies)
)

func getProvider() -> MoyaProvider<MyApi> {
    let provider = MoyaProvider<MyApi>(manager : manager)
    return provider
}

【讨论】:

  • 感谢@lena-bru 的回答。不幸的是,MoyaProvider 不再有带有manager 的初始化程序。这是初始化程序:MoyaProvider(endpointClosure: requestClosure: stubClosure: callbackQueue: session: plugins: trackInflights:)
  • 你用的是哪个版本的moya?
  • 14.0.0.我也尝试过使用 URLSession,但仍然无法正常工作。我猜这与 ATS 设置有关。我不确定要尝试哪些其他设置
【解决方案2】:

问题出在需要证书的 VM 上。 这是一个关于如何设置的技术问答的链接。我选择使用我们的 VPN。 https://developer.apple.com/library/archive/qa/qa1948/_index.html

【讨论】:

    猜你喜欢
    • 2012-09-20
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 2014-11-17
    • 2015-10-12
    • 1970-01-01
    相关资源
    最近更新 更多