Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Create a login page for re-authing a user #639

Merged
merged 2 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/server/assets/login/_loginscripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
},
headers: { 'X-CSRF-Token': '{{.csrfToken}}' },
contentType: 'application/x-www-form-urlencoded',
{{if not .currentUser}}
success: function(returnData) {
// The user successfully signed in, redirect to realm selection.
window.location.assign('/login/select-realm');
},
{{end}}
error: function(xhr, status, e) {
// There was an error finding the user. Redirect to the
// sign-out page to clear the firebase cookie and any session
Expand Down
26 changes: 21 additions & 5 deletions cmd/server/assets/login/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<form id="login-form" class="floating-form" action="/" method="POST">
<div class="form-label-group">
<input type="email" id="email" name="email" class="form-control" placeholder="Email address" required
autofocus />
autofocus {{if .currentUser}}disabled value="{{.currentUser.Email}}"{{end}}/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there an easy way to escape off this page and log in as someone else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. Added the nav header in.

This is currently a bit of a dead end anyway since it's not linked in anywhere

<label for="email">Email address</label>
</div>

Expand Down Expand Up @@ -90,11 +90,15 @@
// Disable the submit button so we only attempt once.
$submit.prop('disabled', true);

{{if .currentUser}}
let credentials = firebase.auth.EmailAuthProvider.credential($email.val(),$password.val());
firebase.auth().currentUser.reauthenticateWithCredential(credentials)
{{else}}
firebase.auth().signInWithEmailAndPassword($email.val(), $password.val())
{{end}}
.then(function(userCredential) {
flash.clear();
})
.catch(function(error) {
loginSuccess();
}).catch(function(error) {
if (error.code == 'auth/multi-factor-auth-required') {
resolver = error.resolver;
populatePinText(resolver.hints);
Expand Down Expand Up @@ -140,7 +144,9 @@
let multiFactorAssertion = firebase.auth.PhoneMultiFactorGenerator.assertion(cred);
// Complete sign-in.
resolver.resolveSignIn(multiFactorAssertion)
.catch(function(err) {
.then(function(userCredential) {
loginSuccess();
}).catch(function(err) {
flash.clear();
flash.error(err.message);
$submitPin.prop('disabled', false);
Expand Down Expand Up @@ -243,6 +249,16 @@

$factors.append($li);
}

function loginSuccess() {
{{if .loginRedirect}}
window.location.assign('{{.loginRedirect}}');
{{else}}
{{if .currentUser}}
flash.alert('Successfully refreshed auth credentials.');
{{end}}
{{end}}
}
});
</script>
</body>
Expand Down
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func realMain(ctx context.Context) error {
sub.Use(requireAuth)
sub.Use(rateLimit)
sub.Use(loadCurrentRealm)
sub.Handle("/login", loginController.HandleReauth()).Methods("GET")
sub.Handle("/login/select-realm", loginController.HandleSelectRealm()).Methods("GET", "POST")
sub.Handle("/login/change-password", loginController.HandleResetPassword()).Methods("GET")
sub.Handle("/account", loginController.HandleAccountSettings()).Methods("GET")
Expand Down
28 changes: 28 additions & 0 deletions pkg/controller/login/reauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 login defines the controller for the login page.
package login

import (
"net/http"
)

func (c *Controller) HandleReauth() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// No redirect for reauth
c.renderLogin(ctx, w)
})
}