mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-05-11 06:48:11 +00:00
4f7335ed73
* feat: add new logger * refactor: use one struct for context handling and cancellation * refactor: rework logging and config in controllers * refactor: rework logging and config in middlewares * refactor: rework logging and cancellation in services * refactor: rework cli logging * fix: improve logging in routines * feat: use sync groups for better cancellation * refactor: simplify middleware, controller and service init * tests: fix controller tests * tests: use require instead of assert where previous step is required * tests: fix middleware tests * tests: fix service tests * tests: fix context tests * fix: fix typos * feat: add option to enable or disable concurrent listeners * fix: assign public key correctly in oidc server * tests: fix don't try to test logger with char size * fix: coderabbit comments * tests: use filepath join instead of path join * fix: ensure unix socket shutdown doesn't run twice * chore: remove temp lint file
84 lines
2.2 KiB
Go
84 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/tinyauthapp/tinyauth/internal/model"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
type UserinfoExtractor func(client *http.Client, url string) (*model.Claims, error)
|
|
|
|
type OAuthService struct {
|
|
serviceCfg model.OAuthServiceConfig
|
|
config *oauth2.Config
|
|
ctx context.Context
|
|
userinfoExtractor UserinfoExtractor
|
|
id string
|
|
}
|
|
|
|
func NewOAuthService(config model.OAuthServiceConfig, id string, ctx context.Context) *OAuthService {
|
|
httpClient := &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
Transport: &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
InsecureSkipVerify: config.Insecure,
|
|
},
|
|
},
|
|
}
|
|
vctx := context.WithValue(ctx, oauth2.HTTPClient, httpClient)
|
|
|
|
return &OAuthService{
|
|
serviceCfg: config,
|
|
config: &oauth2.Config{
|
|
ClientID: config.ClientID,
|
|
ClientSecret: config.ClientSecret,
|
|
RedirectURL: config.RedirectURL,
|
|
Scopes: config.Scopes,
|
|
Endpoint: oauth2.Endpoint{
|
|
AuthURL: config.AuthURL,
|
|
TokenURL: config.TokenURL,
|
|
},
|
|
},
|
|
ctx: vctx,
|
|
userinfoExtractor: defaultExtractor,
|
|
id: id,
|
|
}
|
|
}
|
|
|
|
func (s *OAuthService) WithUserinfoExtractor(extractor UserinfoExtractor) *OAuthService {
|
|
s.userinfoExtractor = extractor
|
|
return s
|
|
}
|
|
|
|
func (s *OAuthService) Name() string {
|
|
return s.serviceCfg.Name
|
|
}
|
|
|
|
func (s *OAuthService) ID() string {
|
|
return s.id
|
|
}
|
|
|
|
func (s *OAuthService) NewRandom() string {
|
|
// The generate verifier function just creates a random string,
|
|
// so we can use it to generate a random state as well
|
|
random := oauth2.GenerateVerifier()
|
|
return random
|
|
}
|
|
|
|
func (s *OAuthService) GetAuthURL(state string, verifier string) string {
|
|
return s.config.AuthCodeURL(state, oauth2.AccessTypeOnline, oauth2.S256ChallengeOption(verifier))
|
|
}
|
|
|
|
func (s *OAuthService) GetToken(code string, verifier string) (*oauth2.Token, error) {
|
|
return s.config.Exchange(s.ctx, code, oauth2.VerifierOption(verifier))
|
|
}
|
|
|
|
func (s *OAuthService) GetUserinfo(token *oauth2.Token) (*model.Claims, error) {
|
|
client := oauth2.NewClient(s.ctx, oauth2.StaticTokenSource(token))
|
|
return s.userinfoExtractor(client, s.serviceCfg.UserinfoURL)
|
|
}
|