| 
									
										
										
										
											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/>. | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | package auth | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/oidc" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/processing" | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/state" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	/* | 
					
						
							|  |  |  | 		paths prefixed with 'auth' | 
					
						
							|  |  |  | 	*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	AuthSignInPath          = "/sign_in" | 
					
						
							|  |  |  | 	Auth2FAPath             = "/2fa" | 
					
						
							|  |  |  | 	AuthCheckYourEmailPath  = "/check_your_email" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	AuthWaitForApprovalPath = "/wait_for_approval" | 
					
						
							|  |  |  | 	AuthAccountDisabledPath = "/account_disabled" | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	AuthCallbackPath        = "/callback" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* | 
					
						
							|  |  |  | 		paths prefixed with 'oauth' | 
					
						
							|  |  |  | 	*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	OauthAuthorizePath = "/authorize" | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	OauthFinalizePath  = "/finalize" | 
					
						
							|  |  |  | 	OauthOOBTokenPath  = "/oob"   // #nosec G101 else we get a hardcoded credentials warning | 
					
						
							|  |  |  | 	OauthTokenPath     = "/token" // #nosec G101 else we get a hardcoded credentials warning | 
					
						
							| 
									
										
										
										
											2025-04-10 16:24:17 +02:00
										 |  |  | 	OauthRevokePath    = "/revoke" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* | 
					
						
							|  |  |  | 		params / session keys | 
					
						
							|  |  |  | 	*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	callbackStateParam       = "state" | 
					
						
							|  |  |  | 	callbackCodeParam        = "code" | 
					
						
							|  |  |  | 	sessionUserID            = "userid" | 
					
						
							|  |  |  | 	sessionUserIDAwaiting2FA = "userid_awaiting_2fa" | 
					
						
							|  |  |  | 	sessionClientID          = "client_id" | 
					
						
							|  |  |  | 	sessionRedirectURI       = "redirect_uri" | 
					
						
							|  |  |  | 	sessionForceLogin        = "force_login" | 
					
						
							|  |  |  | 	sessionResponseType      = "response_type" | 
					
						
							|  |  |  | 	sessionScope             = "scope" | 
					
						
							|  |  |  | 	sessionInternalState     = "internal_state" | 
					
						
							|  |  |  | 	sessionClientState       = "client_state" | 
					
						
							|  |  |  | 	sessionClaims            = "claims" | 
					
						
							|  |  |  | 	sessionAppID             = "app_id" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Module struct { | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	state     *state.State | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	processor *processing.Processor | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	idp       oidc.IDP | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | // New returns an Auth module which provides | 
					
						
							|  |  |  | // both 'oauth' and 'auth' endpoints. | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | // | 
					
						
							|  |  |  | // It is safe to pass a nil idp if oidc is disabled. | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | func New( | 
					
						
							|  |  |  | 	state *state.State, | 
					
						
							|  |  |  | 	processor *processing.Processor, | 
					
						
							|  |  |  | 	idp oidc.IDP, | 
					
						
							|  |  |  | ) *Module { | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	return &Module{ | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 		state:     state, | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 		processor: processor, | 
					
						
							|  |  |  | 		idp:       idp, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // RouteAuth routes all paths that should have an 'auth' prefix | 
					
						
							|  |  |  | func (m *Module) RouteAuth(attachHandler func(method string, path string, f ...gin.HandlerFunc) gin.IRoutes) { | 
					
						
							|  |  |  | 	attachHandler(http.MethodGet, AuthSignInPath, m.SignInGETHandler) | 
					
						
							|  |  |  | 	attachHandler(http.MethodPost, AuthSignInPath, m.SignInPOSTHandler) | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	attachHandler(http.MethodGet, Auth2FAPath, m.TwoFactorCodeGETHandler) | 
					
						
							|  |  |  | 	attachHandler(http.MethodPost, Auth2FAPath, m.TwoFactorCodePOSTHandler) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	attachHandler(http.MethodGet, AuthCallbackPath, m.CallbackGETHandler) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | // RouteOAuth routes all paths that should have an 'oauth' prefix | 
					
						
							|  |  |  | func (m *Module) RouteOAuth(attachHandler func(method string, path string, f ...gin.HandlerFunc) gin.IRoutes) { | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	attachHandler(http.MethodPost, OauthTokenPath, m.TokenPOSTHandler) | 
					
						
							| 
									
										
										
										
											2025-04-10 16:24:17 +02:00
										 |  |  | 	attachHandler(http.MethodPost, OauthRevokePath, m.TokenRevokePOSTHandler) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	attachHandler(http.MethodGet, OauthAuthorizePath, m.AuthorizeGETHandler) | 
					
						
							|  |  |  | 	attachHandler(http.MethodPost, OauthAuthorizePath, m.AuthorizePOSTHandler) | 
					
						
							|  |  |  | 	attachHandler(http.MethodPost, OauthFinalizePath, m.FinalizePOSTHandler) | 
					
						
							| 
									
										
										
										
											2025-04-07 16:14:41 +02:00
										 |  |  | 	attachHandler(http.MethodGet, OauthOOBTokenPath, m.OOBTokenGETHandler) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | } |