| 
									
										
										
										
											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 ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2025-04-19 21:24:58 +02:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2021-12-11 17:50:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	"github.com/gin-contrib/sessions" | 
					
						
							|  |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							| 
									
										
										
										
											2021-07-23 10:36:28 +02:00
										 |  |  | 	"github.com/google/uuid" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" | 
					
						
							|  |  |  | 	apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							| 
									
										
										
										
											2021-05-08 14:25:55 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2022-10-08 13:49:56 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/oauth" | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | // AuthorizeGETHandler should be served as | 
					
						
							|  |  |  | // GET at https://example.org/oauth/authorize. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The idea here is to present an authorization | 
					
						
							|  |  |  | // page to the user, informing them of the scopes | 
					
						
							|  |  |  | // the application is requesting, with a button | 
					
						
							|  |  |  | // that they have to click to give it permission. | 
					
						
							| 
									
										
										
										
											2021-04-20 18:14:23 +02:00
										 |  |  | func (m *Module) AuthorizeGETHandler(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
										 |  |  | 	s := sessions.Default(c) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// UserID will be set in the session by | 
					
						
							|  |  |  | 	// AuthorizePOSTHandler if the caller has | 
					
						
							|  |  |  | 	// already gone through the auth flow. | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// If it's not set, then we don't yet know | 
					
						
							|  |  |  | 	// yet who the user is, so send them to the | 
					
						
							|  |  |  | 	// sign in page first. | 
					
						
							|  |  |  | 	if userID, ok := s.Get(sessionUserID).(string); !ok || userID == "" { | 
					
						
							|  |  |  | 		m.redirectAuthFormToSignIn(c) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	user := m.mustUserFromSession(c, s) | 
					
						
							|  |  |  | 	if user == nil { | 
					
						
							|  |  |  | 		// Error already | 
					
						
							|  |  |  | 		// written. | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// If the user is unconfirmed, waiting approval, | 
					
						
							|  |  |  | 	// or suspended, redirect to an appropriate help page. | 
					
						
							|  |  |  | 	if !m.validateUser(c, user) { | 
					
						
							|  |  |  | 		// Already | 
					
						
							|  |  |  | 		// redirected. | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// Everything looks OK. | 
					
						
							|  |  |  | 	// Start preparing to render the html template. | 
					
						
							|  |  |  | 	instance, errWithCode := m.processor.InstanceGetV1(c.Request.Context()) | 
					
						
							|  |  |  | 	if errWithCode != nil { | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) | 
					
						
							| 
									
										
										
										
											2022-02-07 11:04:31 +00:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	redirectURI := m.mustStringFromSession(c, s, sessionRedirectURI) | 
					
						
							|  |  |  | 	if redirectURI == "" { | 
					
						
							|  |  |  | 		// Error already | 
					
						
							|  |  |  | 		// written. | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	scope := m.mustStringFromSession(c, s, sessionScope) | 
					
						
							|  |  |  | 	if scope == "" { | 
					
						
							|  |  |  | 		// Error already | 
					
						
							|  |  |  | 		// written. | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	app := m.mustAppFromSession(c, s) | 
					
						
							|  |  |  | 	if app == nil { | 
					
						
							|  |  |  | 		// Error already | 
					
						
							|  |  |  | 		// written. | 
					
						
							| 
									
										
										
										
											2022-08-08 10:40:51 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-27 11:23:52 +01:00
										 |  |  | 	// The authorize template will display a form | 
					
						
							|  |  |  | 	// to the user where they can see some info | 
					
						
							|  |  |  | 	// about the app that's trying to authorize, | 
					
						
							|  |  |  | 	// and the scope of the request. They can then | 
					
						
							|  |  |  | 	// approve it if it looks OK to them, which | 
					
						
							|  |  |  | 	// will POST to the AuthorizePOSTHandler. | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	apiutil.TemplateWebPage(c, apiutil.WebPage{ | 
					
						
							| 
									
										
										
										
											2023-12-27 11:23:52 +01:00
										 |  |  | 		Template: "authorize.tmpl", | 
					
						
							|  |  |  | 		Instance: instance, | 
					
						
							|  |  |  | 		Extra: map[string]any{ | 
					
						
							|  |  |  | 			"appname":    app.Name, | 
					
						
							|  |  |  | 			"appwebsite": app.Website, | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 			"redirect":   redirectURI, | 
					
						
							| 
									
										
										
										
											2023-12-27 11:23:52 +01:00
										 |  |  | 			"scope":      scope, | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 			"user":       user.Account.Username, | 
					
						
							| 
									
										
										
										
											2023-12-27 11:23:52 +01:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | // AuthorizePOSTHandler should be served as | 
					
						
							|  |  |  | // POST at https://example.org/oauth/authorize. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // At this point we assume that the user has signed | 
					
						
							|  |  |  | // in and permitted the app to act on their behalf. | 
					
						
							|  |  |  | // We should proceed with the authentication flow | 
					
						
							|  |  |  | // and generate an oauth code at the redirect URI. | 
					
						
							| 
									
										
										
										
											2021-04-20 18:14:23 +02:00
										 |  |  | func (m *Module) AuthorizePOSTHandler(c *gin.Context) { | 
					
						
							| 
									
										
										
										
											2021-07-08 11:32:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// We need to use the session cookie to | 
					
						
							|  |  |  | 	// recreate the original form submitted | 
					
						
							|  |  |  | 	// to the authorizeGEThandler so that it | 
					
						
							|  |  |  | 	// can be validated by the oauth2 library. | 
					
						
							|  |  |  | 	s := sessions.Default(c) | 
					
						
							| 
									
										
										
										
											2021-07-08 11:32:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	responseType := m.mustStringFromSession(c, s, sessionResponseType) | 
					
						
							|  |  |  | 	if responseType == "" { | 
					
						
							|  |  |  | 		// Error already | 
					
						
							|  |  |  | 		// written. | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-08 11:32:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	clientID := m.mustStringFromSession(c, s, sessionClientID) | 
					
						
							|  |  |  | 	if clientID == "" { | 
					
						
							|  |  |  | 		// Error already | 
					
						
							|  |  |  | 		// written. | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-08 11:32:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	redirectURI := m.mustStringFromSession(c, s, sessionRedirectURI) | 
					
						
							|  |  |  | 	if redirectURI == "" { | 
					
						
							|  |  |  | 		// Error already | 
					
						
							|  |  |  | 		// written. | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2022-07-28 16:43:27 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	scope := m.mustStringFromSession(c, s, sessionScope) | 
					
						
							|  |  |  | 	if scope == "" { | 
					
						
							|  |  |  | 		// Error already | 
					
						
							|  |  |  | 		// written. | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	user := m.mustUserFromSession(c, s) | 
					
						
							|  |  |  | 	if user == nil { | 
					
						
							|  |  |  | 		// Error already | 
					
						
							|  |  |  | 		// written. | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-07 15:46:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// Force login is optional with default of "false". | 
					
						
							|  |  |  | 	forceLogin, ok := s.Get(sessionForceLogin).(string) | 
					
						
							|  |  |  | 	if !ok || forceLogin == "" { | 
					
						
							|  |  |  | 		forceLogin = "false" | 
					
						
							| 
									
										
										
										
											2022-02-07 11:04:31 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// Client state is optional with default of "". | 
					
						
							|  |  |  | 	var clientState string | 
					
						
							|  |  |  | 	if cs, ok := s.Get(sessionClientState).(string); ok { | 
					
						
							|  |  |  | 		clientState = cs | 
					
						
							| 
									
										
										
										
											2022-02-07 11:04:31 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// If the user is unconfirmed, waiting approval, | 
					
						
							|  |  |  | 	// or suspended, redirect to an appropriate help page. | 
					
						
							|  |  |  | 	if !m.validateUser(c, user) { | 
					
						
							|  |  |  | 		// Already | 
					
						
							|  |  |  | 		// redirected. | 
					
						
							| 
									
										
										
										
											2022-02-07 11:04:31 +00:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// If we're redirecting to our OOB token handler, | 
					
						
							|  |  |  | 	// we need to keep the session around so the OOB | 
					
						
							|  |  |  | 	// handler can extract values from it. Otherwise, | 
					
						
							|  |  |  | 	// we're going to be redirecting somewhere else | 
					
						
							|  |  |  | 	// so we can safely clear the session now. | 
					
						
							| 
									
										
										
										
											2022-10-08 13:49:56 +02:00
										 |  |  | 	if redirectURI != oauth.OOBURI { | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 		m.mustClearSession(s) | 
					
						
							| 
									
										
										
										
											2022-10-08 13:49:56 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-23 10:36:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// Set values on the request form so that | 
					
						
							|  |  |  | 	// they're picked up by the oauth server. | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 	c.Request.Form = url.Values{ | 
					
						
							|  |  |  | 		sessionResponseType: {responseType}, | 
					
						
							|  |  |  | 		sessionClientID:     {clientID}, | 
					
						
							|  |  |  | 		sessionRedirectURI:  {redirectURI}, | 
					
						
							|  |  |  | 		sessionScope:        {scope}, | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 		sessionUserID:       {user.ID}, | 
					
						
							|  |  |  | 		sessionForceLogin:   {forceLogin}, | 
					
						
							| 
									
										
										
										
											2021-07-07 15:46:42 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-28 16:43:27 +02:00
										 |  |  | 	if clientState != "" { | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 		// If client state was submitted, | 
					
						
							|  |  |  | 		// set it on the form so it can be | 
					
						
							|  |  |  | 		// fed back to the client via a query | 
					
						
							|  |  |  | 		// param at the eventual redirect URL. | 
					
						
							| 
									
										
										
										
											2022-07-28 16:43:27 +02:00
										 |  |  | 		c.Request.Form.Set("state", clientState) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// If OAuthHandleAuthorizeRequest is successful, | 
					
						
							|  |  |  | 	// it'll handle any further redirects for us, | 
					
						
							|  |  |  | 	// but we do still need to handle any errors. | 
					
						
							|  |  |  | 	errWithCode := m.processor.OAuthHandleAuthorizeRequest(c.Writer, c.Request) | 
					
						
							|  |  |  | 	if errWithCode != nil { | 
					
						
							| 
									
										
										
										
											2023-02-02 14:08:13 +01:00
										 |  |  | 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | // redirectAuthFormToSignIn binds an OAuthAuthorize form, | 
					
						
							| 
									
										
										
										
											2025-04-19 21:24:58 +02:00
										 |  |  | // presumed to be set as url query params, stores the values | 
					
						
							|  |  |  | // into the session, and redirects the user to the sign in page. | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | func (m *Module) redirectAuthFormToSignIn(c *gin.Context) { | 
					
						
							|  |  |  | 	s := sessions.Default(c) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	form := &apimodel.OAuthAuthorize{} | 
					
						
							|  |  |  | 	if err := c.ShouldBind(form); err != nil { | 
					
						
							|  |  |  | 		m.clearSessionWithBadRequest(c, s, err, err.Error(), oauth.HelpfulAdvice) | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-19 21:24:58 +02:00
										 |  |  | 	// If scope isn't set default to read. | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// Else massage submitted scope(s) from | 
					
						
							|  |  |  | 	// '+'-separated to space-separated. | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	if form.Scope == "" { | 
					
						
							|  |  |  | 		form.Scope = "read" | 
					
						
							| 
									
										
										
										
											2025-04-19 21:24:58 +02:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		form.Scope = strings.ReplaceAll(form.Scope, "+", " ") | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	// Save these values from the form so we | 
					
						
							|  |  |  | 	// can use them elsewhere in the session. | 
					
						
							| 
									
										
										
										
											2021-07-08 11:32:31 +02:00
										 |  |  | 	s.Set(sessionForceLogin, form.ForceLogin) | 
					
						
							|  |  |  | 	s.Set(sessionResponseType, form.ResponseType) | 
					
						
							|  |  |  | 	s.Set(sessionClientID, form.ClientID) | 
					
						
							|  |  |  | 	s.Set(sessionRedirectURI, form.RedirectURI) | 
					
						
							|  |  |  | 	s.Set(sessionScope, form.Scope) | 
					
						
							| 
									
										
										
										
											2022-07-28 16:43:27 +02:00
										 |  |  | 	s.Set(sessionInternalState, uuid.NewString()) | 
					
						
							|  |  |  | 	s.Set(sessionClientState, form.State) | 
					
						
							| 
									
										
										
										
											2022-06-08 20:38:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	m.mustSaveSession(s) | 
					
						
							|  |  |  | 	c.Redirect(http.StatusSeeOther, "/auth"+AuthSignInPath) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-02-07 11:04:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | // validateUser checks if the given user: | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | //  1. Has a confirmed email address. | 
					
						
							|  |  |  | //  2. Has been approved. | 
					
						
							|  |  |  | //  3. Is not disabled or suspended. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // If all looks OK, returns true. Otherwise, | 
					
						
							|  |  |  | // redirects to a help page and returns false. | 
					
						
							|  |  |  | func (m *Module) validateUser( | 
					
						
							|  |  |  | 	c *gin.Context, | 
					
						
							|  |  |  | 	user *gtsmodel.User, | 
					
						
							|  |  |  | ) bool { | 
					
						
							|  |  |  | 	switch { | 
					
						
							|  |  |  | 	case user.ConfirmedAt.IsZero(): | 
					
						
							|  |  |  | 		// User email not confirmed yet. | 
					
						
							|  |  |  | 		const redirectTo = "/auth" + AuthCheckYourEmailPath | 
					
						
							|  |  |  | 		c.Redirect(http.StatusSeeOther, redirectTo) | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case !*user.Approved: | 
					
						
							|  |  |  | 		// User signup not approved yet. | 
					
						
							|  |  |  | 		const redirectTo = "/auth" + AuthWaitForApprovalPath | 
					
						
							|  |  |  | 		c.Redirect(http.StatusSeeOther, redirectTo) | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case *user.Disabled || !user.Account.SuspendedAt.IsZero(): | 
					
						
							|  |  |  | 		// User disabled or suspended. | 
					
						
							|  |  |  | 		const redirectTo = "/auth" + AuthAccountDisabledPath | 
					
						
							|  |  |  | 		c.Redirect(http.StatusSeeOther, redirectTo) | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		// All good. | 
					
						
							|  |  |  | 		return true | 
					
						
							| 
									
										
										
										
											2022-02-07 11:04:31 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |