mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 05:37:05 +02:00
OAuth2Strategy interface to abstract the details of the login flow
This allows implementing clients the deviate from the standard OAuth2 flow
This commit is contained in:
parent
780af98e1e
commit
f447a259d4
6 changed files with 130 additions and 27 deletions
56
internal/auth/strategy.go
Normal file
56
internal/auth/strategy.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
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 auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
type OAuth2Strategy interface {
|
||||
Config() oauth2.Config
|
||||
|
||||
AuthCodeURL(verifier string, state string) string
|
||||
|
||||
ExchangeToken(code CodeResponse, verifier string) (*oauth2.Token, error)
|
||||
}
|
||||
|
||||
type CodeResponse struct {
|
||||
Code string
|
||||
State string
|
||||
}
|
||||
|
||||
func NewStandardStrategy(conf oauth2.Config) OAuth2Strategy {
|
||||
return StandardStrategy{conf: conf}
|
||||
}
|
||||
|
||||
type StandardStrategy struct {
|
||||
conf oauth2.Config
|
||||
}
|
||||
|
||||
func (s StandardStrategy) Config() oauth2.Config {
|
||||
return s.conf
|
||||
}
|
||||
|
||||
func (s StandardStrategy) AuthCodeURL(verifier string, state string) string {
|
||||
return s.conf.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier))
|
||||
}
|
||||
|
||||
func (s StandardStrategy) ExchangeToken(code CodeResponse, verifier string) (*oauth2.Token, error) {
|
||||
ctx := context.Background()
|
||||
return s.conf.Exchange(ctx, code.Code, oauth2.VerifierOption(verifier))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue