2021-04-01 21:46:45 +03:00
|
|
|
/*
|
|
|
|
GoToSocial
|
|
|
|
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
|
|
|
|
|
|
|
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 16:53:34 +03:00
|
|
|
package security
|
2021-04-01 21:46:45 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-10-11 15:37:33 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-05-21 16:48:26 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-05-08 15:25:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-04-01 21:46:45 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
|
|
|
)
|
|
|
|
|
2021-11-27 16:53:34 +03:00
|
|
|
// TokenCheck checks if the client has presented a valid oauth Bearer token.
|
2021-04-01 21:46:45 +03: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 16:53:34 +03:00
|
|
|
func (m *Module) TokenCheck(c *gin.Context) {
|
2021-10-11 15:37:33 +03:00
|
|
|
l := logrus.WithField("func", "OauthTokenMiddleware")
|
2021-11-27 16:53:34 +03:00
|
|
|
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 21:46:45 +03:00
|
|
|
|
2021-05-15 12:58:11 +03:00
|
|
|
ti, err := m.server.ValidationBearerToken(c.Copy().Request)
|
2021-04-01 21:46:45 +03:00
|
|
|
if err != nil {
|
2021-11-27 16:53:34 +03:00
|
|
|
l.Infof("token was passed in Authorization header but we could not validate it: %s", err)
|
2021-04-01 21:46:45 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Set(oauth.SessionAuthorizedToken, ti)
|
|
|
|
|
|
|
|
// check for user-level token
|
2021-11-27 16:53:34 +03:00
|
|
|
if userID := ti.GetUserID(); userID != "" {
|
|
|
|
l.Tracef("authenticated user %s with bearer token, scope is %s", userID, ti.GetScope())
|
2021-04-01 21:46:45 +03:00
|
|
|
|
2021-11-27 16:53:34 +03:00
|
|
|
// fetch user for this token
|
2021-04-19 20:42:19 +03:00
|
|
|
user := >smodel.User{}
|
2021-11-27 16:53:34 +03:00
|
|
|
if err := m.db.GetByID(ctx, userID, user); err != nil {
|
|
|
|
if err != db.ErrNoEntries {
|
|
|
|
l.Errorf("database error looking for user with id %s: %s", userID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l.Warnf("no user found for userID %s", userID)
|
2021-04-01 21:46:45 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Set(oauth.SessionAuthorizedUser, user)
|
|
|
|
|
2021-11-27 16:53:34 +03:00
|
|
|
// fetch account for this token
|
|
|
|
acct, err := m.db.GetAccountByID(ctx, user.AccountID)
|
|
|
|
if err != nil {
|
|
|
|
if err != db.ErrNoEntries {
|
|
|
|
l.Errorf("database error looking for account with id %s: %s", user.AccountID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l.Warnf("no account found for userID %s", userID)
|
2021-04-01 21:46:45 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Set(oauth.SessionAuthorizedAccount, acct)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for application token
|
2021-11-27 16:53:34 +03:00
|
|
|
if clientID := ti.GetClientID(); clientID != "" {
|
|
|
|
l.Tracef("authenticated client %s with bearer token, scope is %s", clientID, ti.GetScope())
|
|
|
|
|
|
|
|
// fetch app for this token
|
2021-04-19 20:42:19 +03:00
|
|
|
app := >smodel.Application{}
|
2021-11-27 16:53:34 +03:00
|
|
|
if err := m.db.GetWhere(ctx, []db.Where{{Key: "client_id", Value: clientID}}, app); err != nil {
|
|
|
|
if err != db.ErrNoEntries {
|
|
|
|
l.Errorf("database error looking for application with clientID %s: %s", clientID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l.Warnf("no app found for client %s", clientID)
|
|
|
|
return
|
2021-04-01 21:46:45 +03:00
|
|
|
}
|
|
|
|
c.Set(oauth.SessionAuthorizedApplication, app)
|
|
|
|
}
|
|
|
|
}
|