| 
									
										
										
										
											2023-03-12 16:00:57 +01:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | // GNU Affero General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | package auth | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	"slices" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-26 15:34:10 +02:00
										 |  |  | 	apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/config" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/gtserror" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/oauth" | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	"codeberg.org/gruf/go-byteutil" | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	"github.com/gin-contrib/sessions" | 
					
						
							|  |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	"github.com/pquerna/otp/totp" | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	"golang.org/x/crypto/bcrypt" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | // SignInGETHandler should be served at | 
					
						
							|  |  |  | // GET https://example.org/auth/sign_in. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The idea is to present a friendly sign-in | 
					
						
							|  |  |  | // page to the user, where they can enter their | 
					
						
							|  |  |  | // username and password. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // When submitted, the form will POST to the sign- | 
					
						
							|  |  |  | // in page, which will be handled by SignInPOSTHandler. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // If an idp provider is set, then the user will | 
					
						
							|  |  |  | // be redirected to that to do their sign in. | 
					
						
							| 
									
										
										
										
											2021-04-20 18:14:23 +02:00
										 |  |  | func (m *Module) SignInGETHandler(c *gin.Context) { | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	if _, err := apiutil.NegotiateAccept(c, apiutil.HTMLAcceptHeaders...); err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 		apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1) | 
					
						
							| 
									
										
										
										
											2021-12-11 17:50:00 +01:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	if config.GetOIDCEnabled() { | 
					
						
							|  |  |  | 		// IDP provider is in use, so redirect to it | 
					
						
							|  |  |  | 		// instead of serving our own sign in page. | 
					
						
							|  |  |  | 		// | 
					
						
							|  |  |  | 		// We need the internal state to know where | 
					
						
							|  |  |  | 		// to redirect to. | 
					
						
							|  |  |  | 		internalState := m.mustStringFromSession( | 
					
						
							|  |  |  | 			c, | 
					
						
							|  |  |  | 			sessions.Default(c), | 
					
						
							|  |  |  | 			sessionInternalState, | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		if internalState == "" { | 
					
						
							|  |  |  | 			// Error already | 
					
						
							|  |  |  | 			// written. | 
					
						
							| 
									
										
										
										
											2022-08-08 10:40:51 +02:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 		c.Redirect(http.StatusSeeOther, m.idp.AuthCodeURL(internalState)) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-23 10:36:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// IDP provider is not in use. | 
					
						
							|  |  |  | 	// Render our own cute little page. | 
					
						
							|  |  |  | 	instance, errWithCode := m.processor.InstanceGetV1(c.Request.Context()) | 
					
						
							|  |  |  | 	if errWithCode != nil { | 
					
						
							|  |  |  | 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) | 
					
						
							| 
									
										
										
										
											2021-07-23 10:36:28 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	apiutil.TemplateWebPage(c, apiutil.WebPage{ | 
					
						
							|  |  |  | 		Template: "sign-in.tmpl", | 
					
						
							|  |  |  | 		Instance: instance, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | // SignInPOSTHandler should be served at | 
					
						
							|  |  |  | // POST https://example.org/auth/sign_in. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The handler will check the submitted credentials, | 
					
						
							|  |  |  | // then redirect either to the 2fa form, or straight | 
					
						
							|  |  |  | // to the authorize page served at /oauth/authorize. | 
					
						
							| 
									
										
										
										
											2021-04-20 18:14:23 +02:00
										 |  |  | func (m *Module) SignInPOSTHandler(c *gin.Context) { | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	s := sessions.Default(c) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// Parse email + password. | 
					
						
							|  |  |  | 	form := &struct { | 
					
						
							| 
									
										
										
										
											2025-04-19 21:57:50 +02:00
										 |  |  | 		Email    string `form:"username" binding:"required"` | 
					
						
							|  |  |  | 		Password string `form:"password" binding:"required"` | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	}{} | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	if err := c.ShouldBind(form); err != nil { | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 		m.clearSessionWithBadRequest(c, s, err, oauth.HelpfulAdvice) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	user, errWithCode := m.validatePassword( | 
					
						
							|  |  |  | 		c.Request.Context(), | 
					
						
							|  |  |  | 		form.Email, | 
					
						
							|  |  |  | 		form.Password, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	if errWithCode != nil { | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 		// Don't clear session here yet, so the user | 
					
						
							|  |  |  | 		// can just press back and try again if they | 
					
						
							|  |  |  | 		// accidentally gave the wrong password, without | 
					
						
							|  |  |  | 		// having to do the whole sign in flow again! | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// Whether or not 2fa is enabled, we want | 
					
						
							|  |  |  | 	// to save the session when we're done here. | 
					
						
							|  |  |  | 	defer m.mustSaveSession(s) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if user.TwoFactorEnabled() { | 
					
						
							|  |  |  | 		// If this user has 2FA enabled, redirect | 
					
						
							|  |  |  | 		// to the 2FA page and have them submit | 
					
						
							|  |  |  | 		// a code from their authenticator app. | 
					
						
							|  |  |  | 		s.Set(sessionUserIDAwaiting2FA, user.ID) | 
					
						
							|  |  |  | 		c.Redirect(http.StatusFound, "/auth"+Auth2FAPath) | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// If the user doesn't have 2fa enabled, | 
					
						
							|  |  |  | 	// redirect straight to the OAuth authorize page. | 
					
						
							|  |  |  | 	s.Set(sessionUserID, user.ID) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	c.Redirect(http.StatusFound, "/oauth"+OauthAuthorizePath) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | // validatePassword takes an email address and a password. | 
					
						
							|  |  |  | // The func authenticates the password against the one for | 
					
						
							|  |  |  | // that email address stored in the database. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // If OK, it returns the user, so that it can be used in | 
					
						
							|  |  |  | // further OAuth flows to generate a token etc. | 
					
						
							|  |  |  | func (m *Module) validatePassword( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							|  |  |  | 	email string, | 
					
						
							|  |  |  | 	password string, | 
					
						
							|  |  |  | ) (*gtsmodel.User, gtserror.WithCode) { | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	if email == "" || password == "" { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		err := errors.New("email or password was not provided") | 
					
						
							|  |  |  | 		return incorrectPassword(err) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	user, err := m.state.DB.GetUserByEmailAddress(ctx, email) | 
					
						
							| 
									
										
										
										
											2022-10-03 10:46:11 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		err := fmt.Errorf("user %s was not retrievable from db during oauth authorization attempt: %s", email, err) | 
					
						
							|  |  |  | 		return incorrectPassword(err) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	if user.EncryptedPassword == "" { | 
					
						
							|  |  |  | 		err := fmt.Errorf("encrypted password for user %s was empty for some reason", user.Email) | 
					
						
							|  |  |  | 		return incorrectPassword(err) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	if err := bcrypt.CompareHashAndPassword( | 
					
						
							|  |  |  | 		byteutil.S2B(user.EncryptedPassword), | 
					
						
							|  |  |  | 		byteutil.S2B(password), | 
					
						
							|  |  |  | 	); err != nil { | 
					
						
							| 
									
										
										
										
											2023-12-27 11:23:52 +01:00
										 |  |  | 		err := fmt.Errorf("password hash didn't match for user %s during sign in attempt: %s", user.Email, err) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		return incorrectPassword(err) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	return user, nil | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | // incorrectPassword wraps the given error in a gtserror.WithCode, and returns | 
					
						
							|  |  |  | // only a generic 'safe' error message to the user, to not give any info away. | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | func incorrectPassword(err error) (*gtsmodel.User, gtserror.WithCode) { | 
					
						
							|  |  |  | 	const errText = "password/email combination was incorrect" | 
					
						
							|  |  |  | 	return nil, gtserror.NewErrorUnauthorized(err, errText, oauth.HelpfulAdvice) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TwoFactorCodeGETHandler should be served at | 
					
						
							|  |  |  | // GET https://example.org/auth/2fa. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The 2fa template displays a simple form asking the | 
					
						
							|  |  |  | // user to input a code from their authenticator app. | 
					
						
							|  |  |  | func (m *Module) TwoFactorCodeGETHandler(c *gin.Context) { | 
					
						
							|  |  |  | 	s := sessions.Default(c) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	user := m.mustUserFromSession(c, s) | 
					
						
							|  |  |  | 	if user == nil { | 
					
						
							|  |  |  | 		// Error already | 
					
						
							|  |  |  | 		// written. | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	instance, errWithCode := m.processor.InstanceGetV1(c.Request.Context()) | 
					
						
							|  |  |  | 	if errWithCode != nil { | 
					
						
							|  |  |  | 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apiutil.TemplateWebPage(c, apiutil.WebPage{ | 
					
						
							|  |  |  | 		Template: "2fa.tmpl", | 
					
						
							|  |  |  | 		Instance: instance, | 
					
						
							|  |  |  | 		Extra: map[string]any{ | 
					
						
							|  |  |  | 			"user": user.Account.Username, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TwoFactorCodePOSTHandler should be served at | 
					
						
							|  |  |  | // POST https://example.org/auth/2fa. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The idea is to handle a submitted 2fa code, validate it, | 
					
						
							|  |  |  | // and if valid redirect to the /oauth/authorize page that | 
					
						
							|  |  |  | // the user would get to if they didn't have 2fa enabled. | 
					
						
							|  |  |  | func (m *Module) TwoFactorCodePOSTHandler(c *gin.Context) { | 
					
						
							|  |  |  | 	s := sessions.Default(c) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	user := m.mustUserFromSession(c, s) | 
					
						
							|  |  |  | 	if user == nil { | 
					
						
							|  |  |  | 		// Error already | 
					
						
							|  |  |  | 		// written. | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Parse 2fa code. | 
					
						
							|  |  |  | 	form := &struct { | 
					
						
							| 
									
										
										
										
											2025-04-19 21:57:50 +02:00
										 |  |  | 		Code string `form:"code" binding:"required"` | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	}{} | 
					
						
							|  |  |  | 	if err := c.ShouldBind(form); err != nil { | 
					
						
							|  |  |  | 		m.clearSessionWithBadRequest(c, s, err, oauth.HelpfulAdvice) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	valid, err := m.validate2FACode(c, user, form.Code) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		m.clearSessionWithInternalError(c, s, err, oauth.HelpfulAdvice) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !valid { | 
					
						
							|  |  |  | 		// Don't clear session here yet, so the user | 
					
						
							|  |  |  | 		// can just press back and try again if they | 
					
						
							|  |  |  | 		// accidentally gave the wrong code, without | 
					
						
							|  |  |  | 		// having to do the whole sign in flow again! | 
					
						
							|  |  |  | 		const errText = "2fa code invalid or timed out, press back and try again; " + | 
					
						
							|  |  |  | 			"if issues persist, pester your instance admin to check the server clock" | 
					
						
							|  |  |  | 		errWithCode := gtserror.NewErrorBadRequest(errors.New(errText), errText) | 
					
						
							|  |  |  | 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Code looks good! Redirect | 
					
						
							|  |  |  | 	// to the OAuth authorize page. | 
					
						
							|  |  |  | 	s.Set(sessionUserID, user.ID) | 
					
						
							|  |  |  | 	m.mustSaveSession(s) | 
					
						
							|  |  |  | 	c.Redirect(http.StatusFound, "/oauth"+OauthAuthorizePath) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (m *Module) validate2FACode(c *gin.Context, user *gtsmodel.User, code string) (bool, error) { | 
					
						
							|  |  |  | 	code = strings.TrimSpace(code) | 
					
						
							|  |  |  | 	if len(code) <= 6 { | 
					
						
							|  |  |  | 		// This is a normal authenticator | 
					
						
							|  |  |  | 		// app code, just try to validate it. | 
					
						
							|  |  |  | 		return totp.Validate(code, user.TwoFactorSecret), nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// This is a one-time recovery code. | 
					
						
							|  |  |  | 	// Check against the user's stored codes. | 
					
						
							|  |  |  | 	for i := 0; i < len(user.TwoFactorBackups); i++ { | 
					
						
							|  |  |  | 		err := bcrypt.CompareHashAndPassword( | 
					
						
							|  |  |  | 			byteutil.S2B(user.TwoFactorBackups[i]), | 
					
						
							|  |  |  | 			byteutil.S2B(code), | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			// Doesn't match, | 
					
						
							|  |  |  | 			// try next. | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// We have a match. | 
					
						
							|  |  |  | 		// Remove this one-time code from the user's backups. | 
					
						
							|  |  |  | 		user.TwoFactorBackups = slices.Delete(user.TwoFactorBackups, i, i+1) | 
					
						
							|  |  |  | 		if err := m.state.DB.UpdateUser( | 
					
						
							|  |  |  | 			c.Request.Context(), | 
					
						
							|  |  |  | 			user, | 
					
						
							|  |  |  | 			"two_factor_backups", | 
					
						
							|  |  |  | 		); err != nil { | 
					
						
							|  |  |  | 			return false, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// So valid bestie! | 
					
						
							|  |  |  | 		return true, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Not a valid one-time | 
					
						
							|  |  |  | 	// recovery code. | 
					
						
							|  |  |  | 	return false, nil | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | } |