| 
									
										
										
										
											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 middleware | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-25 15:15:36 +02:00
										 |  |  | 	"code.superseriousbusiness.org/oauth2/v4" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/oauth" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TokenCheck returns a new gin middleware for validating oauth tokens in requests. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The middleware checks the request Authorization header for a valid oauth Bearer token. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // If no token was set in the Authorization header, or the token was invalid, the handler will return. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // If a valid oauth Bearer token was provided, it will be set on the gin context for further use. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Then, it will check which *gtsmodel.User the token belongs to. If the user is not confirmed, not approved, | 
					
						
							|  |  |  | // or has been disabled, then the middleware will return early. Otherwise, the User will be set on the | 
					
						
							|  |  |  | // gin context for further processing by other functions. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Next, it will look up the *gtsmodel.Account for the User. If the Account has been suspended, then the | 
					
						
							|  |  |  | // middleware will return early. Otherwise, it will set the Account on the gin context too. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Finally, it will check the client ID of the token to see if a *gtsmodel.Application can be retrieved | 
					
						
							|  |  |  | // for that client ID. This will also be set on the gin context. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // If an invalid token is presented, or a user/account/application can't be found, then this middleware | 
					
						
							|  |  |  | // won't abort the request, since the server might want to still allow public requests that don't have a | 
					
						
							|  |  |  | // Bearer token set (eg., for public instance information and so on). | 
					
						
							|  |  |  | func TokenCheck(dbConn db.DB, validateBearerToken func(r *http.Request) (oauth2.TokenInfo, error)) func(*gin.Context) { | 
					
						
							|  |  |  | 	return func(c *gin.Context) { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 		// Acquire context from gin request. | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 		ctx := c.Request.Context() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if c.Request.Header.Get("Authorization") == "" { | 
					
						
							|  |  |  | 			// no token set in the header, we can just bail | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ti, err := validateBearerToken(c.Copy().Request) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 			log.Debugf(ctx, "token was passed in Authorization header but we could not validate it: %s", err) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		c.Set(oauth.SessionAuthorizedToken, ti) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// check for user-level token | 
					
						
							|  |  |  | 		if userID := ti.GetUserID(); userID != "" { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 			log.Tracef(ctx, "authenticated user %s with bearer token, scope is %s", userID, ti.GetScope()) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// fetch user for this token | 
					
						
							|  |  |  | 			user, err := dbConn.GetUserByID(ctx, userID) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				if err != db.ErrNoEntries { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 					log.Errorf(ctx, "database error looking for user with id %s: %s", userID, err) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 					return | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 				log.Warnf(ctx, "no user found for userID %s", userID) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if user.ConfirmedAt.IsZero() { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 				log.Warnf(ctx, "authenticated user %s has never confirmed thier email address", userID) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if !*user.Approved { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 				log.Warnf(ctx, "authenticated user %s's account was never approved by an admin", userID) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if *user.Disabled { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 				log.Warnf(ctx, "authenticated user %s's account was disabled'", userID) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			c.Set(oauth.SessionAuthorizedUser, user) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// fetch account for this token | 
					
						
							|  |  |  | 			if user.Account == nil { | 
					
						
							|  |  |  | 				acct, err := dbConn.GetAccountByID(ctx, user.AccountID) | 
					
						
							|  |  |  | 				if err != nil { | 
					
						
							|  |  |  | 					if err != db.ErrNoEntries { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 						log.Errorf(ctx, "database error looking for account with id %s: %s", user.AccountID, err) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 						return | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 					log.Warnf(ctx, "no account found for userID %s", userID) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 					return | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				user.Account = acct | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if !user.Account.SuspendedAt.IsZero() { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 				log.Warnf(ctx, "authenticated user %s's account (accountId=%s) has been suspended", userID, user.AccountID) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			c.Set(oauth.SessionAuthorizedAccount, user.Account) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// check for application token | 
					
						
							|  |  |  | 		if clientID := ti.GetClientID(); clientID != "" { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 			log.Tracef(ctx, "authenticated client %s with bearer token, scope is %s", clientID, ti.GetScope()) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// fetch app for this token | 
					
						
							| 
									
										
										
										
											2023-08-10 15:08:41 +01:00
										 |  |  | 			app, err := dbConn.GetApplicationByClientID(ctx, clientID) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 				if err != db.ErrNoEntries { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 					log.Errorf(ctx, "database error looking for application with clientID %s: %s", clientID, err) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 					return | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 				log.Warnf(ctx, "no app found for client %s", clientID) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-08-10 15:08:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 			c.Set(oauth.SessionAuthorizedApplication, app) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |