This repository was archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
Introduce auth provider interface #902
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,12 +38,13 @@ | |
} | ||
|
||
firebase.auth().applyActionCode(code) | ||
.then(function(resp) { | ||
window.location.assign("/login/manage-account?mode=verifyEmail"); | ||
}).catch(function(error) { | ||
flash.error("Invalid email verification code. " | ||
+ "The code may be malformed, expired, or has already been used."); | ||
}); | ||
.then(function(resp) { | ||
window.location.assign("/"); | ||
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. The previous implementation assumed that the user was logged in when they verified their email. It's possible they were on a different computer (e.g. desktop but opened on mobile) or incognito. The result would be a successful verification, then an unauthorized page. This fixes it by redirecting people to the home page (after which our normal flow picks up). |
||
}).catch(function(error) { | ||
flash.clear(); | ||
flash.error("Invalid email verification code. " | ||
+ "The code may be malformed, expired, or has already been used."); | ||
}); | ||
}); | ||
|
||
function getUrlVars() { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,12 +32,12 @@ | |
</div> | ||
{{end}} | ||
|
||
<p>Email address ownership for <em>{{.currentUser.Email}}</em> is <strong id="not">not</strong> confirmed. | ||
</p> | ||
<p>Email address ownership for <em>{{.currentUser.Email}}</em> is <strong id="not">not</strong> confirmed.</p> | ||
|
||
<form method="POST"> | ||
<form method="POST" id="verify-email"> | ||
{{ .csrfField }} | ||
<button id="verify" class="btn btn-primary btn-block" disabled>Send verification email</button> | ||
<input type="submit" id="verify-button" class="btn btn-primary btn-block" | ||
value="Send verification email" disabled> | ||
|
||
<small class="form-text text-muted"> | ||
Click to send an email containing a verification link. | ||
|
@@ -56,35 +56,41 @@ | |
</div> | ||
</main> | ||
|
||
{{if .firebase}} | ||
<script> | ||
let $verify = $('#verify'); | ||
let $skip = $('#skip'); | ||
let $not = $('#not'); | ||
let $form = $("#verify-email"); | ||
let $verifyButton = $("#verify-button"); | ||
let $skip = $("#skip"); | ||
let $not = $("#not"); | ||
|
||
firebase.auth().onAuthStateChanged(function(user) { | ||
if (!user) { | ||
window.location.assign("/signout"); | ||
return; | ||
} | ||
|
||
if ({{if eq .sendInvite "sent"}}true{{else}}user.emailVerified{{end}}) { | ||
$not.hide(); | ||
$skip.text("Go home"); | ||
} else { | ||
$verify.prop('disabled', false); | ||
// If the email is already verified, move along. | ||
if (user.emailVerified) { | ||
flash.clear(); | ||
flash.alert("Your email address is already verified."); | ||
window.location.assign("/home"); | ||
return; | ||
} | ||
|
||
{{if eq .sendInvite "send"}} | ||
if (!user.emailVerified) { | ||
user.sendEmailVerification().then(function() { | ||
flash.clear(); | ||
flash.alert('Verification email sent.'); | ||
$verify.prop('disabled', true); | ||
}); | ||
} | ||
{{end}} | ||
// Get an ID token and embed it onto the page. | ||
user.getIdToken().then(idToken => { | ||
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. There's technically a race here. If the user leaves this page open for a few hours, the code will expire. I think it's fine. |
||
let $idTokenField = $("<input>"); | ||
$idTokenField.attr("type", "hidden"); | ||
$idTokenField.attr("name", "idToken"); | ||
$idTokenField.attr("value", idToken); | ||
$form.append($idTokenField); | ||
|
||
// Now that we have a user, enable the form. | ||
$verifyButton.prop("disabled", false); | ||
}); | ||
}); | ||
</script> | ||
{{end}} | ||
</body> | ||
|
||
</html> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package auth exposes interfaces for various auth methods. | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gorilla/sessions" | ||
) | ||
|
||
var ( | ||
ErrSessionMissing = fmt.Errorf("session is missing") | ||
) | ||
|
||
// InviteUserEmailFunc sends email with the given inviteLink. | ||
type InviteUserEmailFunc func(ctx context.Context, inviteLink string) error | ||
|
||
// ResetPasswordEmailFunc is an email composer function. | ||
type ResetPasswordEmailFunc func(ctx context.Context, resetLink string) error | ||
|
||
// EmailVerificationEmailFunc is an email composer function. | ||
type EmailVerificationEmailFunc func(ctx context.Context, verifyLink string) error | ||
|
||
// Provider is a generic authentication provider interface. | ||
type Provider interface { | ||
// StoreSession stores the session in the values. | ||
StoreSession(context.Context, *sessions.Session, *SessionInfo) error | ||
|
||
// CheckRevoked checks if the auth has been revoked. It returns an error if | ||
// the auth does not exist or if the auth has been revoked. | ||
CheckRevoked(context.Context, *sessions.Session) error | ||
|
||
// ClearSession removes any information about this auth from the session. | ||
ClearSession(context.Context, *sessions.Session) | ||
|
||
// CreateUser creates a user in the auth provider. If pass is "", the provider | ||
// creates and uses a random password. | ||
CreateUser(ctx context.Context, name, email, pass string, composer InviteUserEmailFunc) (bool, error) | ||
|
||
// SendResetPasswordEmail resets the given user's password. If the user does not exist, | ||
// the underlying provider determines whether itls an error or perhaps upserts | ||
// the account. | ||
SendResetPasswordEmail(ctx context.Context, email string, composer ResetPasswordEmailFunc) error | ||
|
||
// ChangePassword changes the users password. The additional authentication | ||
// information is provider-specific. | ||
ChangePassword(ctx context.Context, newPassword string, data interface{}) error | ||
|
||
// VerifyPasswordResetCode verifies the code is valid. It returns the email of | ||
// the user for which the code belongs. | ||
VerifyPasswordResetCode(ctx context.Context, code string) (string, error) | ||
|
||
// SendEmailVerificationEmail sends the email verification email for the | ||
// currently authenticated user. Data is arbitrary additional data that the | ||
// provider might need (like user ID) to send the verification. | ||
SendEmailVerificationEmail(ctx context.Context, email string, data interface{}, composer EmailVerificationEmailFunc) error | ||
|
||
// EmailAddress extracts the email address for this auth provider from the | ||
// session. It returns an error if the session does not exist. | ||
EmailAddress(context.Context, *sessions.Session) (string, error) | ||
|
||
// EmailVerified returns true if the current user is verified, false | ||
// otherwise. | ||
EmailVerified(context.Context, *sessions.Session) (bool, error) | ||
|
||
// MFAEnabled returns true if MFA is enabled, false otherwise. | ||
MFAEnabled(context.Context, *sessions.Session) (bool, error) | ||
} | ||
|
||
// SessionInfo is a generic struct used to store session information. Not all | ||
// providers use all fields. | ||
type SessionInfo struct { | ||
// IDToken is a unique string or ID. It is usually a JWT token. | ||
IDToken string | ||
|
||
// TTL is the session duration. | ||
TTL time.Duration | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is available in the cookie now, we don't need to pull it from the javascript.