2021-04-01 19:46:45 +01:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 17:42:19 +00:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-04-01 19:46:45 +01: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 13:53:34 +00:00
|
|
|
package security
|
2021-04-01 19:46:45 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-05-21 14:48:26 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-05-08 13:25:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-07-19 09:47:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-04-01 19:46:45 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
|
|
|
)
|
|
|
|
|
2021-11-27 13:53:34 +00:00
|
|
|
// TokenCheck checks if the client has presented a valid oauth Bearer token.
|
2021-04-01 19:46:45 +01: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 13:53:34 +00: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 19:46:45 +01:00
|
|
|
|
2021-05-15 10:58:11 +01:00
|
|
|
ti, err := m.server.ValidationBearerToken(c.Copy().Request)
|
2021-04-01 19:46:45 +01:00
|
|
|
if err != nil {
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Infof("token was passed in Authorization header but we could not validate it: %s", err)
|
2021-04-01 19:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Set(oauth.SessionAuthorizedToken, ti)
|
|
|
|
|
|
|
|
// check for user-level token
|
2021-11-27 13:53:34 +00:00
|
|
|
if userID := ti.GetUserID(); userID != "" {
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Tracef("authenticated user %s with bearer token, scope is %s", userID, ti.GetScope())
|
2021-04-01 19:46:45 +01:00
|
|
|
|
2021-11-27 13:53:34 +00:00
|
|
|
// fetch user for this token
|
2021-04-19 18:42:19 +01:00
|
|
|
user := >smodel.User{}
|
2021-11-27 13:53:34 +00:00
|
|
|
if err := m.db.GetByID(ctx, userID, user); err != nil {
|
|
|
|
if err != db.ErrNoEntries {
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Errorf("database error looking for user with id %s: %s", userID, err)
|
2021-11-27 13:53:34 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Warnf("no user found for userID %s", userID)
|
2021-04-01 19:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
2022-02-07 11:04:31 +00:00
|
|
|
|
|
|
|
if user.ConfirmedAt.IsZero() {
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Warnf("authenticated user %s has never confirmed thier email address", userID)
|
2022-02-07 11:04:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-15 11:35:05 +01:00
|
|
|
if !*user.Approved {
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Warnf("authenticated user %s's account was never approved by an admin", userID)
|
2022-02-07 11:04:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-15 11:35:05 +01:00
|
|
|
if *user.Disabled {
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Warnf("authenticated user %s's account was disabled'", userID)
|
2022-02-07 11:04:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-01 19:46:45 +01:00
|
|
|
c.Set(oauth.SessionAuthorizedUser, user)
|
|
|
|
|
2021-11-27 13:53:34 +00:00
|
|
|
// fetch account for this token
|
|
|
|
acct, err := m.db.GetAccountByID(ctx, user.AccountID)
|
|
|
|
if err != nil {
|
|
|
|
if err != db.ErrNoEntries {
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Errorf("database error looking for account with id %s: %s", user.AccountID, err)
|
2021-11-27 13:53:34 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Warnf("no account found for userID %s", userID)
|
2021-04-01 19:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
2022-02-07 11:04:31 +00:00
|
|
|
|
|
|
|
if !acct.SuspendedAt.IsZero() {
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Warnf("authenticated user %s's account (accountId=%s) has been suspended", userID, user.AccountID)
|
2022-02-07 11:04:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-01 19:46:45 +01:00
|
|
|
c.Set(oauth.SessionAuthorizedAccount, acct)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for application token
|
2021-11-27 13:53:34 +00:00
|
|
|
if clientID := ti.GetClientID(); clientID != "" {
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Tracef("authenticated client %s with bearer token, scope is %s", clientID, ti.GetScope())
|
2021-11-27 13:53:34 +00:00
|
|
|
|
|
|
|
// fetch app for this token
|
2021-04-19 18:42:19 +01:00
|
|
|
app := >smodel.Application{}
|
2021-11-27 13:53:34 +00:00
|
|
|
if err := m.db.GetWhere(ctx, []db.Where{{Key: "client_id", Value: clientID}}, app); err != nil {
|
|
|
|
if err != db.ErrNoEntries {
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Errorf("database error looking for application with clientID %s: %s", clientID, err)
|
2021-11-27 13:53:34 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-19 09:47:55 +01:00
|
|
|
log.Warnf("no app found for client %s", clientID)
|
2021-11-27 13:53:34 +00:00
|
|
|
return
|
2021-04-01 19:46:45 +01:00
|
|
|
}
|
|
|
|
c.Set(oauth.SessionAuthorizedApplication, app)
|
|
|
|
}
|
|
|
|
}
|