2021-04-01 20:46:45 +02:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 18:42:19 +01:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-04-01 20:46:45 +02:00
|
|
|
|
|
|
|
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-11-27 14:53:34 +01:00
|
|
|
package security
|
2021-04-01 20:46:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-05-21 15:48:26 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-07-19 10:47:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-04-01 20:46:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
|
|
|
)
|
|
|
|
|
2021-11-27 14:53:34 +01:00
|
|
|
// TokenCheck checks if the client has presented a valid oauth Bearer token.
|
2021-04-01 20:46:45 +02:00
|
|
|
// If so, it will check the User that the token belongs to, and set that in the context of
|
|
|
|
// the request. Then, it will look up the account for that user, and set that in the request too.
|
|
|
|
// If user or account can't be found, then the handler won't *fail*, in case the server wants to allow
|
|
|
|
// public requests that don't have a Bearer token set (eg., for public instance information and so on).
|
2021-11-27 14:53:34 +01:00
|
|
|
func (m *Module) TokenCheck(c *gin.Context) {
|
|
|
|
ctx := c.Request.Context()
|
|
|
|
defer c.Next()
|
|
|
|
|
|
|
|
if c.Request.Header.Get("Authorization") == "" {
|
|
|
|
// no token set in the header, we can just bail
|
|
|
|
return
|
|
|
|
}
|
2021-04-01 20:46:45 +02:00
|
|
|
|
2021-05-15 11:58:11 +02:00
|
|
|
ti, err := m.server.ValidationBearerToken(c.Copy().Request)
|
2021-04-01 20:46:45 +02:00
|
|
|
if err != nil {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Infof("token was passed in Authorization header but we could not validate it: %s", err)
|
2021-04-01 20:46:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Set(oauth.SessionAuthorizedToken, ti)
|
|
|
|
|
|
|
|
// check for user-level token
|
2021-11-27 14:53:34 +01:00
|
|
|
if userID := ti.GetUserID(); userID != "" {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Tracef("authenticated user %s with bearer token, scope is %s", userID, ti.GetScope())
|
2021-04-01 20:46:45 +02:00
|
|
|
|
2021-11-27 14:53:34 +01:00
|
|
|
// fetch user for this token
|
2022-10-03 10:46:11 +02:00
|
|
|
user, err := m.db.GetUserByID(ctx, userID)
|
|
|
|
if err != nil {
|
2021-11-27 14:53:34 +01:00
|
|
|
if err != db.ErrNoEntries {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("database error looking for user with id %s: %s", userID, err)
|
2021-11-27 14:53:34 +01:00
|
|
|
return
|
|
|
|
}
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Warnf("no user found for userID %s", userID)
|
2021-04-01 20:46:45 +02:00
|
|
|
return
|
|
|
|
}
|
2022-02-07 12:04:31 +01:00
|
|
|
|
|
|
|
if user.ConfirmedAt.IsZero() {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Warnf("authenticated user %s has never confirmed thier email address", userID)
|
2022-02-07 12:04:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-15 12:35:05 +02:00
|
|
|
if !*user.Approved {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Warnf("authenticated user %s's account was never approved by an admin", userID)
|
2022-02-07 12:04:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-15 12:35:05 +02:00
|
|
|
if *user.Disabled {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Warnf("authenticated user %s's account was disabled'", userID)
|
2022-02-07 12:04:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-01 20:46:45 +02:00
|
|
|
c.Set(oauth.SessionAuthorizedUser, user)
|
|
|
|
|
2021-11-27 14:53:34 +01:00
|
|
|
// fetch account for this token
|
2022-10-03 10:46:11 +02:00
|
|
|
if user.Account == nil {
|
|
|
|
acct, err := m.db.GetAccountByID(ctx, user.AccountID)
|
|
|
|
if err != nil {
|
|
|
|
if err != db.ErrNoEntries {
|
|
|
|
log.Errorf("database error looking for account with id %s: %s", user.AccountID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Warnf("no account found for userID %s", userID)
|
2021-11-27 14:53:34 +01:00
|
|
|
return
|
|
|
|
}
|
2022-10-03 10:46:11 +02:00
|
|
|
user.Account = acct
|
2021-04-01 20:46:45 +02:00
|
|
|
}
|
2022-02-07 12:04:31 +01:00
|
|
|
|
2022-10-03 10:46:11 +02:00
|
|
|
if !user.Account.SuspendedAt.IsZero() {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Warnf("authenticated user %s's account (accountId=%s) has been suspended", userID, user.AccountID)
|
2022-02-07 12:04:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-03 10:46:11 +02:00
|
|
|
c.Set(oauth.SessionAuthorizedAccount, user.Account)
|
2021-04-01 20:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for application token
|
2021-11-27 14:53:34 +01:00
|
|
|
if clientID := ti.GetClientID(); clientID != "" {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Tracef("authenticated client %s with bearer token, scope is %s", clientID, ti.GetScope())
|
2021-11-27 14:53:34 +01:00
|
|
|
|
|
|
|
// fetch app for this token
|
2021-04-19 19:42:19 +02:00
|
|
|
app := >smodel.Application{}
|
2021-11-27 14:53:34 +01:00
|
|
|
if err := m.db.GetWhere(ctx, []db.Where{{Key: "client_id", Value: clientID}}, app); err != nil {
|
|
|
|
if err != db.ErrNoEntries {
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Errorf("database error looking for application with clientID %s: %s", clientID, err)
|
2021-11-27 14:53:34 +01:00
|
|
|
return
|
|
|
|
}
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Warnf("no app found for client %s", clientID)
|
2021-11-27 14:53:34 +01:00
|
|
|
return
|
2021-04-01 20:46:45 +02:00
|
|
|
}
|
|
|
|
c.Set(oauth.SessionAuthorizedApplication, app)
|
|
|
|
}
|
|
|
|
}
|