-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[google_sign_in] Enable FedCM for web. Use token expiration. #5225
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,32 +52,51 @@ Map<String, Object?>? decodeJwtPayload(String? payload) { | |
return null; | ||
} | ||
|
||
/// Returns the payload of a [CredentialResponse]. | ||
Map<String, Object?>? getResponsePayload(CredentialResponse? response) { | ||
if (response?.credential == null) { | ||
return null; | ||
} | ||
|
||
return getJwtTokenPayload(response!.credential); | ||
} | ||
|
||
/// Converts a [CredentialResponse] into a [GoogleSignInUserData]. | ||
/// | ||
/// May return `null`, if the `credentialResponse` is null, or its `credential` | ||
/// cannot be decoded. | ||
GoogleSignInUserData? gisResponsesToUserData( | ||
CredentialResponse? credentialResponse) { | ||
if (credentialResponse == null || credentialResponse.credential == null) { | ||
return null; | ||
} | ||
|
||
final Map<String, Object?>? payload = | ||
getJwtTokenPayload(credentialResponse.credential); | ||
|
||
final Map<String, Object?>? payload = getResponsePayload(credentialResponse); | ||
if (payload == null) { | ||
return null; | ||
} | ||
|
||
assert(credentialResponse?.credential != null, | ||
'The CredentialResponse cannot be null and have a payload.'); | ||
|
||
return GoogleSignInUserData( | ||
email: payload['email']! as String, | ||
id: payload['sub']! as String, | ||
displayName: payload['name'] as String?, | ||
photoUrl: payload['picture'] as String?, | ||
idToken: credentialResponse.credential, | ||
idToken: credentialResponse!.credential, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is conceptually removed enough from the code that ensures that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, in this case I think what I really want here is an assertion that (credentialResponse?.credential != null) before returning the GoogleSignInUserData, that way I'm expressing in code that the credential must NOT be null before returning a GoogleSignInUserData. |
||
); | ||
} | ||
|
||
/// Returns the expiration timestamp ('exp') of a [CredentialResponse]. | ||
/// | ||
/// May return `null` if the `credentialResponse` is null, its `credential` | ||
/// cannot be decoded, or the `exp` field is not set on the JWT payload. | ||
DateTime? getCredentialResponseExpirationTimestamp( | ||
CredentialResponse? credentialResponse) { | ||
final Map<String, Object?>? payload = getResponsePayload(credentialResponse); | ||
// Get the 'exp' field from the payload, if present. | ||
final int? exp = (payload != null) ? payload['exp'] as int? : null; | ||
// Return 'exp' (a timestamp in seconds since Epoch) as a DateTime. | ||
return (exp != null) ? DateTime.fromMillisecondsSinceEpoch(exp * 1000) : null; | ||
} | ||
|
||
/// Converts responses from the GIS library into TokenData for the plugin. | ||
GoogleSignInTokenData gisResponsesToTokenData( | ||
CredentialResponse? credentialResponse, TokenResponse? tokenResponse) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So a missing
expiration
field would indicate that it's already expired? Naively (not knowing the API surface) I would expect that if expiration is optional, then not having it should yield atrue
result here, so maybe a comment would be helpful?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to document this a little bit further, but in this case, if the
CredentialResponse
payload cannot be decoded, is null, or doesn't contain an "exp" claim, we assume that the token is bad, and thus the user is not authenticated.In a valid Google ID Token, the "exp" claim should always be provided:
https://developers.google.com/identity/openid-connect/openid-connect#an-id-tokens-payload