2021-09-29 18:19:34 +02:00
|
|
|
//go:generate mockgen --build_flags=--mod=mod -destination=mocks/mockauth_interface.go -package mocks . AuthInterface
|
2021-02-02 21:06:28 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2021-02-03 03:15:03 +01:00
|
|
|
"database/sql"
|
2021-02-02 21:06:28 +01:00
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/model"
|
|
|
|
"github.com/mattermost/focalboard/server/services/config"
|
|
|
|
"github.com/mattermost/focalboard/server/services/store"
|
2021-10-07 13:51:01 +02:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2021-02-02 21:06:28 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2021-09-29 18:19:34 +02:00
|
|
|
type AuthInterface interface {
|
|
|
|
GetSession(token string) (*model.Session, error)
|
|
|
|
IsValidReadToken(c store.Container, blockID string, readToken string) (bool, error)
|
|
|
|
DoesUserHaveWorkspaceAccess(userID string, workspaceID string) bool
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// Auth authenticates sessions.
|
2021-02-02 21:06:28 +01:00
|
|
|
type Auth struct {
|
|
|
|
config *config.Configuration
|
|
|
|
store store.Store
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// New returns a new Auth.
|
2021-02-02 21:06:28 +01:00
|
|
|
func New(config *config.Configuration, store store.Store) *Auth {
|
|
|
|
return &Auth{config: config, store: store}
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// GetSession Get a user active session and refresh the session if needed.
|
2021-02-02 21:06:28 +01:00
|
|
|
func (a *Auth) GetSession(token string) (*model.Session, error) {
|
|
|
|
if len(token) < 1 {
|
|
|
|
return nil, errors.New("no session token")
|
|
|
|
}
|
|
|
|
|
|
|
|
session, err := a.store.GetSession(token, a.config.SessionExpireTime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "unable to get the session for the token")
|
|
|
|
}
|
2021-10-07 13:51:01 +02:00
|
|
|
if session.UpdateAt < (utils.GetMillis() - utils.SecondsToMillis(a.config.SessionRefreshTime)) {
|
2021-06-21 11:21:42 +02:00
|
|
|
_ = a.store.RefreshSession(session)
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
return session, nil
|
|
|
|
}
|
2021-02-03 03:15:03 +01:00
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// IsValidReadToken validates the read token for a block.
|
2021-03-26 19:01:54 +01:00
|
|
|
func (a *Auth) IsValidReadToken(c store.Container, blockID string, readToken string) (bool, error) {
|
|
|
|
rootID, err := a.store.GetRootID(c, blockID)
|
2021-02-03 03:15:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
sharing, err := a.store.GetSharing(c, rootID)
|
2021-06-21 11:21:42 +02:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2021-02-03 03:15:03 +01:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sharing != nil && (sharing.ID == rootID && sharing.Enabled && sharing.Token == readToken) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
2021-05-24 19:06:11 +02:00
|
|
|
|
|
|
|
func (a *Auth) DoesUserHaveWorkspaceAccess(userID string, workspaceID string) bool {
|
|
|
|
hasAccess, err := a.store.HasWorkspaceAccess(userID, workspaceID)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return hasAccess
|
|
|
|
}
|