Moved tokensource to auth package

This commit is contained in:
Philipp Wolfer 2023-11-24 10:30:11 +01:00
parent 46e6a667c8
commit 5aed552854
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
2 changed files with 8 additions and 7 deletions

View file

@ -22,6 +22,7 @@ import (
"strings"
"github.com/spf13/viper"
"go.uploadedlobster.com/scotty/internal/auth"
"go.uploadedlobster.com/scotty/internal/models"
"go.uploadedlobster.com/scotty/internal/storage"
)
@ -36,19 +37,19 @@ func BuildRedirectURL(config *viper.Viper, backend string) (*url.URL, error) {
}
func Authenticate(service string, backend models.Backend, db storage.Database, config *viper.Viper) (bool, error) {
authenticator, auth := backend.(models.OAuth2Authenticator)
if auth {
authenticator, needAuth := backend.(models.OAuth2Authenticator)
if needAuth {
redirectURL, err := BuildRedirectURL(config, backend.Name())
if err != nil {
return auth, err
return needAuth, err
}
token, err := db.GetOAuth2Token(service)
if err != nil {
return auth, err
return needAuth, err
}
conf := authenticator.OAuth2Strategy(redirectURL).Config()
tokenSource := NewDatabaseTokenSource(db, service, &conf, token)
tokenSource := auth.NewDatabaseTokenSource(db, service, &conf, token)
authenticator.OAuth2Setup(tokenSource)
}
return auth, nil
return needAuth, nil
}

View file

@ -1,70 +0,0 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package backends
import (
"context"
"go.uploadedlobster.com/scotty/internal/storage"
"golang.org/x/oauth2"
)
type databaseTokenSource struct {
new oauth2.TokenSource
db storage.Database
service string
tok *oauth2.Token
}
func (s *databaseTokenSource) Token() (tok *oauth2.Token, err error) {
tok = s.tok
if tok == nil {
tok, _ = s.loadToken()
}
if tok != nil && tok.Valid() {
return tok, nil
}
if tok, err = s.new.Token(); err != nil {
return nil, err
}
err = s.saveToken(tok)
s.tok = tok
if err != nil {
return nil, err
}
return tok, err
}
func (c *databaseTokenSource) saveToken(tok *oauth2.Token) error {
return c.db.SetOAuth2Token(c.service, tok)
}
func (c *databaseTokenSource) loadToken() (*oauth2.Token, error) {
return c.db.GetOAuth2Token(c.service)
}
func NewDatabaseTokenSource(db storage.Database, service string, config *oauth2.Config, tok *oauth2.Token) oauth2.TokenSource {
return &databaseTokenSource{
db: db,
new: config.TokenSource(context.Background(), tok),
service: service,
tok: tok,
}
}