使用Firebase Auth,希望使用JWT对发送MongoDB请求的客户端(iOS应用程序)进行身份验证



我目前正在使用Firebase Auth对所有用户进行身份验证,并使用mongodb领域/领域同步/图集作为我的数据库。据我所知,使用两个独立的"auth"(firebase和mongo(并将它们与firebase UID链接作为密钥似乎是个坏主意。因此,JWT似乎是我的最佳选择。

我觉得以下代码还可以,但realm应用程序上的配置不确定:当我使用自定义JWT令牌和JWK URI时,我不断发现公钥不是PEM格式的(https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com(我得到以下错误:

登录失败:无法从URI获取JWK:无法从密钥构造密钥:不支持的kty类型错误域=领域::应用程序:服务错误代码=47";未能从URI获取JWK:未能从密钥构造密钥:不支持的kty类型";UserInfo={NSLocalizedDescription=未能从URI:未能从密钥构造密钥:不支持的kty类型,领域::app::ServiceError=AuthError}

// Function is supposed to auth user using mongo auth by using the firebase jwt
func authJWTMongo(completion: @escaping (Bool) -> Void) -> Void{
let currentUser = Auth.auth().currentUser
currentUser?.getIDTokenForcingRefresh(true) { idToken, error in
if error != nil {
print("Totally f'ed the mongoAuth")
completion(false)
return;
}
// Auth using Mongo shit
print("JWT is< (String(describing: idToken))>")
if let idToken = idToken{
let credentials = Credentials.jwt(token: idToken)
app.login(credentials: credentials) { (result) in
switch result {
case .failure(let error):
print("Login failed: (error.localizedDescription)")
print("(error)")
completion(false)
case .success(let user):
print("Successfully logged in as user (user)")
completion(true)
// Now logged in, do something with user
// Remember to dispatch to main if you are doing anything on the UI thread
}
}
}
}
}

感谢您的帮助。

显然,正确的JWK URI是https://www.googleapis.com/service_accounts/v1/JWK/securetoken@system.gserviceaccount.com

上面的swift代码没有错。

最新更新