Compare commits

...

5 Commits

Author SHA1 Message Date
dependabot[bot]
53473509f8 chore(deps): bump oven/bun from 1.3.0-alpine to 1.3.1-alpine
Bumps oven/bun from 1.3.0-alpine to 1.3.1-alpine.

---
updated-dependencies:
- dependency-name: oven/bun
  dependency-version: 1.3.1-alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-10-22 08:26:11 +00:00
Chris Ellrich
c5bb389258 feat: ACL labels from environment variables (#422)
* feat: add LabelService to retrieve application labels from environment variables

* feat: allow usage of labels from docker and env variables simultaneously

Prioritize labels from environment variables over labels from docker
labels

* fix: handle error returned by label_serive.go/LoadLabels

see https://github.com/steveiliop56/tinyauth/pull/422#discussion_r2443443032

* refactor(label_service): use simple loop instead of slices.ContainsFunc to avoid experimental slices package
see https://github.com/steveiliop56/tinyauth/pull/422#pullrequestreview-3354632045

* refactor: merge acl logic into one service

---------

Co-authored-by: Stavros <steveiliop56@gmail.com>
2025-10-21 16:02:31 +03:00
Stavros
6647c6cd78 refactor: use gorm generics api for database actions 2025-10-19 19:16:53 +03:00
Stavros
7231efcbc3 feat: add routine to cleanup expired sessions 2025-10-19 19:10:24 +03:00
Stavros
5482430907 refactor: generate a verifier on every oauth auth session 2025-10-19 19:03:38 +03:00
12 changed files with 200 additions and 54 deletions

View File

@@ -1,5 +1,5 @@
# Site builder # Site builder
FROM oven/bun:1.3.0-alpine AS frontend-builder FROM oven/bun:1.3.1-alpine AS frontend-builder
WORKDIR /frontend WORKDIR /frontend

View File

@@ -1,5 +1,5 @@
# Site builder # Site builder
FROM oven/bun:1.3.0-alpine AS frontend-builder FROM oven/bun:1.3.1-alpine AS frontend-builder
WORKDIR /frontend WORKDIR /frontend

View File

@@ -2,6 +2,7 @@ package bootstrap
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
@@ -13,11 +14,13 @@ import (
"tinyauth/internal/config" "tinyauth/internal/config"
"tinyauth/internal/controller" "tinyauth/internal/controller"
"tinyauth/internal/middleware" "tinyauth/internal/middleware"
"tinyauth/internal/model"
"tinyauth/internal/service" "tinyauth/internal/service"
"tinyauth/internal/utils" "tinyauth/internal/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"gorm.io/gorm"
) )
type Controller interface { type Controller interface {
@@ -136,12 +139,14 @@ func (app *BootstrapApp) Setup() error {
// Create services // Create services
dockerService := service.NewDockerService() dockerService := service.NewDockerService()
aclsService := service.NewAccessControlsService(dockerService)
authService := service.NewAuthService(authConfig, dockerService, ldapService, database) authService := service.NewAuthService(authConfig, dockerService, ldapService, database)
oauthBrokerService := service.NewOAuthBrokerService(oauthProviders) oauthBrokerService := service.NewOAuthBrokerService(oauthProviders)
// Initialize services // Initialize services (order matters)
services := []Service{ services := []Service{
dockerService, dockerService,
aclsService,
authService, authService,
oauthBrokerService, oauthBrokerService,
} }
@@ -243,7 +248,7 @@ func (app *BootstrapApp) Setup() error {
proxyController := controller.NewProxyController(controller.ProxyControllerConfig{ proxyController := controller.NewProxyController(controller.ProxyControllerConfig{
AppURL: app.config.AppURL, AppURL: app.config.AppURL,
}, apiRouter, dockerService, authService) }, apiRouter, aclsService, authService)
userController := controller.NewUserController(controller.UserControllerConfig{ userController := controller.NewUserController(controller.UserControllerConfig{
CookieDomain: cookieDomain, CookieDomain: cookieDomain,
@@ -277,6 +282,10 @@ func (app *BootstrapApp) Setup() error {
go app.heartbeat() go app.heartbeat()
} }
// Start DB cleanup routine
log.Debug().Msg("Starting database cleanup routine")
go app.dbCleanup(database)
// Start server // Start server
address := fmt.Sprintf("%s:%d", app.config.Address, app.config.Port) address := fmt.Sprintf("%s:%d", app.config.Address, app.config.Port)
log.Info().Msgf("Starting server on %s", address) log.Info().Msgf("Starting server on %s", address)
@@ -338,3 +347,17 @@ func (app *BootstrapApp) heartbeat() {
} }
} }
} }
func (app *BootstrapApp) dbCleanup(db *gorm.DB) {
ticker := time.NewTicker(time.Duration(30) * time.Minute)
defer ticker.Stop()
ctx := context.Background()
for ; true; <-ticker.C {
log.Debug().Msg("Cleaning up old database sessions")
_, err := gorm.G[model.Session](db).Where("expiry < ?", time.Now().UnixMilli()).Delete(ctx)
if err != nil {
log.Error().Err(err).Msg("Failed to cleanup old sessions")
}
}
}

View File

@@ -72,6 +72,7 @@ func (controller *OAuthController) oauthURLHandler(c *gin.Context) {
return return
} }
service.GenerateVerifier()
state := service.GenerateState() state := service.GenerateState()
authURL := service.GetAuthURL(state) authURL := service.GetAuthURL(state)
c.SetCookie(controller.config.CSRFCookieName, state, int(time.Hour.Seconds()), "/", fmt.Sprintf(".%s", controller.config.CookieDomain), controller.config.SecureCookie, true) c.SetCookie(controller.config.CSRFCookieName, state, int(time.Hour.Seconds()), "/", fmt.Sprintf(".%s", controller.config.CookieDomain), controller.config.SecureCookie, true)

View File

@@ -24,15 +24,15 @@ type ProxyControllerConfig struct {
type ProxyController struct { type ProxyController struct {
config ProxyControllerConfig config ProxyControllerConfig
router *gin.RouterGroup router *gin.RouterGroup
docker *service.DockerService acls *service.AccessControlsService
auth *service.AuthService auth *service.AuthService
} }
func NewProxyController(config ProxyControllerConfig, router *gin.RouterGroup, docker *service.DockerService, auth *service.AuthService) *ProxyController { func NewProxyController(config ProxyControllerConfig, router *gin.RouterGroup, acls *service.AccessControlsService, auth *service.AuthService) *ProxyController {
return &ProxyController{ return &ProxyController{
config: config, config: config,
router: router, router: router,
docker: docker, acls: acls,
auth: auth, auth: auth,
} }
} }
@@ -76,20 +76,21 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
proto := c.Request.Header.Get("X-Forwarded-Proto") proto := c.Request.Header.Get("X-Forwarded-Proto")
host := c.Request.Header.Get("X-Forwarded-Host") host := c.Request.Header.Get("X-Forwarded-Host")
labels, err := controller.docker.GetLabels(host) // Get acls
acls, err := controller.acls.GetAccessControls(host)
if err != nil { if err != nil {
log.Error().Err(err).Msg("Failed to get labels from Docker") log.Error().Err(err).Msg("Failed to get access controls for resource")
controller.handleError(c, req, isBrowser) controller.handleError(c, req, isBrowser)
return return
} }
log.Trace().Interface("labels", labels).Msg("Labels for resource") log.Trace().Interface("acls", acls).Msg("ACLs for resource")
clientIP := c.ClientIP() clientIP := c.ClientIP()
if controller.auth.IsBypassedIP(labels.IP, clientIP) { if controller.auth.IsBypassedIP(acls.IP, clientIP) {
controller.setHeaders(c, labels) controller.setHeaders(c, acls)
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"status": 200, "status": 200,
"message": "Authenticated", "message": "Authenticated",
@@ -97,7 +98,7 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
return return
} }
authEnabled, err := controller.auth.IsAuthEnabled(uri, labels.Path) authEnabled, err := controller.auth.IsAuthEnabled(uri, acls.Path)
if err != nil { if err != nil {
log.Error().Err(err).Msg("Failed to check if auth is enabled for resource") log.Error().Err(err).Msg("Failed to check if auth is enabled for resource")
@@ -107,7 +108,7 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
if !authEnabled { if !authEnabled {
log.Debug().Msg("Authentication disabled for resource, allowing access") log.Debug().Msg("Authentication disabled for resource, allowing access")
controller.setHeaders(c, labels) controller.setHeaders(c, acls)
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"status": 200, "status": 200,
"message": "Authenticated", "message": "Authenticated",
@@ -115,7 +116,7 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
return return
} }
if !controller.auth.CheckIP(labels.IP, clientIP) { if !controller.auth.CheckIP(acls.IP, clientIP) {
if req.Proxy == "nginx" || !isBrowser { if req.Proxy == "nginx" || !isBrowser {
c.JSON(401, gin.H{ c.JSON(401, gin.H{
"status": 401, "status": 401,
@@ -160,7 +161,7 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
} }
if userContext.IsLoggedIn { if userContext.IsLoggedIn {
appAllowed := controller.auth.IsResourceAllowed(c, userContext, labels) appAllowed := controller.auth.IsResourceAllowed(c, userContext, acls)
if !appAllowed { if !appAllowed {
log.Warn().Str("user", userContext.Username).Str("resource", strings.Split(host, ".")[0]).Msg("User not allowed to access resource") log.Warn().Str("user", userContext.Username).Str("resource", strings.Split(host, ".")[0]).Msg("User not allowed to access resource")
@@ -194,7 +195,7 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
} }
if userContext.OAuth { if userContext.OAuth {
groupOK := controller.auth.IsInOAuthGroup(c, userContext, labels.OAuth.Groups) groupOK := controller.auth.IsInOAuthGroup(c, userContext, acls.OAuth.Groups)
if !groupOK { if !groupOK {
log.Warn().Str("user", userContext.Username).Str("resource", strings.Split(host, ".")[0]).Msg("User OAuth groups do not match resource requirements") log.Warn().Str("user", userContext.Username).Str("resource", strings.Split(host, ".")[0]).Msg("User OAuth groups do not match resource requirements")
@@ -234,7 +235,7 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
c.Header("Remote-Email", utils.SanitizeHeader(userContext.Email)) c.Header("Remote-Email", utils.SanitizeHeader(userContext.Email))
c.Header("Remote-Groups", utils.SanitizeHeader(userContext.OAuthGroups)) c.Header("Remote-Groups", utils.SanitizeHeader(userContext.OAuthGroups))
controller.setHeaders(c, labels) controller.setHeaders(c, acls)
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"status": 200, "status": 200,
@@ -264,21 +265,21 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/login?%s", controller.config.AppURL, queries.Encode())) c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/login?%s", controller.config.AppURL, queries.Encode()))
} }
func (controller *ProxyController) setHeaders(c *gin.Context, labels config.App) { func (controller *ProxyController) setHeaders(c *gin.Context, acls config.App) {
c.Header("Authorization", c.Request.Header.Get("Authorization")) c.Header("Authorization", c.Request.Header.Get("Authorization"))
headers := utils.ParseHeaders(labels.Response.Headers) headers := utils.ParseHeaders(acls.Response.Headers)
for key, value := range headers { for key, value := range headers {
log.Debug().Str("header", key).Msg("Setting header") log.Debug().Str("header", key).Msg("Setting header")
c.Header(key, value) c.Header(key, value)
} }
basicPassword := utils.GetSecret(labels.Response.BasicAuth.Password, labels.Response.BasicAuth.PasswordFile) basicPassword := utils.GetSecret(acls.Response.BasicAuth.Password, acls.Response.BasicAuth.PasswordFile)
if labels.Response.BasicAuth.Username != "" && basicPassword != "" { if acls.Response.BasicAuth.Username != "" && basicPassword != "" {
log.Debug().Str("username", labels.Response.BasicAuth.Username).Msg("Setting basic auth header") log.Debug().Str("username", acls.Response.BasicAuth.Username).Msg("Setting basic auth header")
c.Header("Authorization", fmt.Sprintf("Basic %s", utils.GetBasicAuth(labels.Response.BasicAuth.Username, basicPassword))) c.Header("Authorization", fmt.Sprintf("Basic %s", utils.GetBasicAuth(acls.Response.BasicAuth.Username, basicPassword)))
} }
} }

View File

@@ -39,6 +39,11 @@ func setupProxyController(t *testing.T, middlewares *[]gin.HandlerFunc) (*gin.En
assert.NilError(t, dockerService.Init()) assert.NilError(t, dockerService.Init())
// Access controls
accessControlsService := service.NewAccessControlsService(dockerService)
assert.NilError(t, accessControlsService.Init())
// Auth service // Auth service
authService := service.NewAuthService(service.AuthServiceConfig{ authService := service.NewAuthService(service.AuthServiceConfig{
Users: []config.User{ Users: []config.User{
@@ -59,7 +64,7 @@ func setupProxyController(t *testing.T, middlewares *[]gin.HandlerFunc) (*gin.En
// Controller // Controller
ctrl := controller.NewProxyController(controller.ProxyControllerConfig{ ctrl := controller.NewProxyController(controller.ProxyControllerConfig{
AppURL: "http://localhost:8080", AppURL: "http://localhost:8080",
}, group, dockerService, authService) }, group, accessControlsService, authService)
ctrl.SetupRoutes() ctrl.SetupRoutes()
return router, recorder, authService return router, recorder, authService

View File

@@ -0,0 +1,103 @@
package service
import (
"os"
"strings"
"tinyauth/internal/config"
"tinyauth/internal/utils/decoders"
"github.com/rs/zerolog/log"
)
type AccessControlsService struct {
docker *DockerService
envACLs config.Apps
}
func NewAccessControlsService(docker *DockerService) *AccessControlsService {
return &AccessControlsService{
docker: docker,
}
}
func (acls *AccessControlsService) Init() error {
acls.envACLs = config.Apps{}
env := os.Environ()
appEnvVars := []string{}
for _, e := range env {
if strings.HasPrefix(e, "TINYAUTH_APPS_") {
appEnvVars = append(appEnvVars, e)
}
}
err := acls.loadEnvACLs(appEnvVars)
if err != nil {
return err
}
return nil
}
func (acls *AccessControlsService) loadEnvACLs(appEnvVars []string) error {
if len(appEnvVars) == 0 {
return nil
}
envAcls := map[string]string{}
for _, e := range appEnvVars {
parts := strings.SplitN(e, "=", 2)
if len(parts) != 2 {
continue
}
// Normalize key, this should use the same normalization logic as in utils/decoders/decoders.go
key := parts[0]
key = strings.ToLower(key)
key = strings.ReplaceAll(key, "_", ".")
value := parts[1]
envAcls[key] = value
}
apps, err := decoders.DecodeLabels(envAcls)
if err != nil {
return err
}
acls.envACLs = apps
return nil
}
func (acls *AccessControlsService) lookupEnvACLs(appDomain string) *config.App {
if len(acls.envACLs.Apps) == 0 {
return nil
}
for appName, appACLs := range acls.envACLs.Apps {
if appACLs.Config.Domain == appDomain {
return &appACLs
}
if strings.SplitN(appDomain, ".", 2)[0] == appName {
return &appACLs
}
}
return nil
}
func (acls *AccessControlsService) GetAccessControls(appDomain string) (config.App, error) {
// First check environment variables
envACLs := acls.lookupEnvACLs(appDomain)
if envACLs != nil {
log.Debug().Str("domain", appDomain).Msg("Found matching access controls in environment variables")
return *envACLs, nil
}
// Fallback to Docker labels
return acls.docker.GetLabels(appDomain)
}

View File

@@ -1,6 +1,8 @@
package service package service
import ( import (
"context"
"errors"
"fmt" "fmt"
"regexp" "regexp"
"strings" "strings"
@@ -41,6 +43,7 @@ type AuthService struct {
loginMutex sync.RWMutex loginMutex sync.RWMutex
ldap *LdapService ldap *LdapService
database *gorm.DB database *gorm.DB
ctx context.Context
} }
func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapService, database *gorm.DB) *AuthService { func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapService, database *gorm.DB) *AuthService {
@@ -54,6 +57,7 @@ func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapS
} }
func (auth *AuthService) Init() error { func (auth *AuthService) Init() error {
auth.ctx = context.Background()
return nil return nil
} }
@@ -213,7 +217,7 @@ func (auth *AuthService) CreateSessionCookie(c *gin.Context, data *config.Sessio
OAuthName: data.OAuthName, OAuthName: data.OAuthName,
} }
err = auth.database.Create(&session).Error err = gorm.G[model.Session](auth.database).Create(auth.ctx, &session)
if err != nil { if err != nil {
return err return err
@@ -231,10 +235,10 @@ func (auth *AuthService) DeleteSessionCookie(c *gin.Context) error {
return err return err
} }
res := auth.database.Unscoped().Where("uuid = ?", cookie).Delete(&model.Session{}) _, err = gorm.G[model.Session](auth.database).Where("uuid = ?", cookie).Delete(auth.ctx)
if res.Error != nil { if err != nil {
return res.Error return err
} }
c.SetCookie(auth.config.SessionCookieName, "", -1, "/", fmt.Sprintf(".%s", auth.config.CookieDomain), auth.config.SecureCookie, true) c.SetCookie(auth.config.SessionCookieName, "", -1, "/", fmt.Sprintf(".%s", auth.config.CookieDomain), auth.config.SecureCookie, true)
@@ -249,15 +253,13 @@ func (auth *AuthService) GetSessionCookie(c *gin.Context) (config.SessionCookie,
return config.SessionCookie{}, err return config.SessionCookie{}, err
} }
var session model.Session session, err := gorm.G[model.Session](auth.database).Where("uuid = ?", cookie).First(auth.ctx)
res := auth.database.Unscoped().Where("uuid = ?", cookie).First(&session) if err != nil {
return config.SessionCookie{}, err
if res.Error != nil {
return config.SessionCookie{}, res.Error
} }
if res.RowsAffected == 0 { if errors.Is(err, gorm.ErrRecordNotFound) {
return config.SessionCookie{}, fmt.Errorf("session not found") return config.SessionCookie{}, fmt.Errorf("session not found")
} }
@@ -287,21 +289,21 @@ func (auth *AuthService) UserAuthConfigured() bool {
return len(auth.config.Users) > 0 || auth.ldap != nil return len(auth.config.Users) > 0 || auth.ldap != nil
} }
func (auth *AuthService) IsResourceAllowed(c *gin.Context, context config.UserContext, labels config.App) bool { func (auth *AuthService) IsResourceAllowed(c *gin.Context, context config.UserContext, acls config.App) bool {
if context.OAuth { if context.OAuth {
log.Debug().Msg("Checking OAuth whitelist") log.Debug().Msg("Checking OAuth whitelist")
return utils.CheckFilter(labels.OAuth.Whitelist, context.Email) return utils.CheckFilter(acls.OAuth.Whitelist, context.Email)
} }
if labels.Users.Block != "" { if acls.Users.Block != "" {
log.Debug().Msg("Checking blocked users") log.Debug().Msg("Checking blocked users")
if utils.CheckFilter(labels.Users.Block, context.Username) { if utils.CheckFilter(acls.Users.Block, context.Username) {
return false return false
} }
} }
log.Debug().Msg("Checking users") log.Debug().Msg("Checking users")
return utils.CheckFilter(labels.Users.Allow, context.Username) return utils.CheckFilter(acls.Users.Allow, context.Username)
} }
func (auth *AuthService) IsInOAuthGroup(c *gin.Context, context config.UserContext, requiredGroups string) bool { func (auth *AuthService) IsInOAuthGroup(c *gin.Context, context config.UserContext, requiredGroups string) bool {
@@ -369,8 +371,8 @@ func (auth *AuthService) GetBasicAuth(c *gin.Context) *config.User {
} }
} }
func (auth *AuthService) CheckIP(labels config.AppIP, ip string) bool { func (auth *AuthService) CheckIP(acls config.AppIP, ip string) bool {
for _, blocked := range labels.Block { for _, blocked := range acls.Block {
res, err := utils.FilterIP(blocked, ip) res, err := utils.FilterIP(blocked, ip)
if err != nil { if err != nil {
log.Warn().Err(err).Str("item", blocked).Msg("Invalid IP/CIDR in block list") log.Warn().Err(err).Str("item", blocked).Msg("Invalid IP/CIDR in block list")
@@ -382,7 +384,7 @@ func (auth *AuthService) CheckIP(labels config.AppIP, ip string) bool {
} }
} }
for _, allowed := range labels.Allow { for _, allowed := range acls.Allow {
res, err := utils.FilterIP(allowed, ip) res, err := utils.FilterIP(allowed, ip)
if err != nil { if err != nil {
log.Warn().Err(err).Str("item", allowed).Msg("Invalid IP/CIDR in allow list") log.Warn().Err(err).Str("item", allowed).Msg("Invalid IP/CIDR in allow list")
@@ -394,7 +396,7 @@ func (auth *AuthService) CheckIP(labels config.AppIP, ip string) bool {
} }
} }
if len(labels.Allow) > 0 { if len(acls.Allow) > 0 {
log.Debug().Str("ip", ip).Msg("IP not in allow list, denying access") log.Debug().Str("ip", ip).Msg("IP not in allow list, denying access")
return false return false
} }
@@ -403,8 +405,8 @@ func (auth *AuthService) CheckIP(labels config.AppIP, ip string) bool {
return true return true
} }
func (auth *AuthService) IsBypassedIP(labels config.AppIP, ip string) bool { func (auth *AuthService) IsBypassedIP(acls config.AppIP, ip string) bool {
for _, bypassed := range labels.Bypass { for _, bypassed := range acls.Bypass {
res, err := utils.FilterIP(bypassed, ip) res, err := utils.FilterIP(bypassed, ip)
if err != nil { if err != nil {
log.Warn().Err(err).Str("item", bypassed).Msg("Invalid IP/CIDR in bypass list") log.Warn().Err(err).Str("item", bypassed).Msg("Invalid IP/CIDR in bypass list")

View File

@@ -59,10 +59,8 @@ func (generic *GenericOAuthService) Init() error {
ctx := context.Background() ctx := context.Background()
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient) ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
verifier := oauth2.GenerateVerifier()
generic.context = ctx generic.context = ctx
generic.verifier = verifier
return nil return nil
} }
@@ -76,6 +74,12 @@ func (generic *GenericOAuthService) GenerateState() string {
return state return state
} }
func (generic *GenericOAuthService) GenerateVerifier() string {
verifier := oauth2.GenerateVerifier()
generic.verifier = verifier
return verifier
}
func (generic *GenericOAuthService) GetAuthURL(state string) string { func (generic *GenericOAuthService) GetAuthURL(state string) string {
return generic.config.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(generic.verifier)) return generic.config.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(generic.verifier))
} }

View File

@@ -53,10 +53,7 @@ func (github *GithubOAuthService) Init() error {
httpClient := &http.Client{} httpClient := &http.Client{}
ctx := context.Background() ctx := context.Background()
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient) ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
verifier := oauth2.GenerateVerifier()
github.context = ctx github.context = ctx
github.verifier = verifier
return nil return nil
} }
@@ -70,6 +67,12 @@ func (github *GithubOAuthService) GenerateState() string {
return state return state
} }
func (github *GithubOAuthService) GenerateVerifier() string {
verifier := oauth2.GenerateVerifier()
github.verifier = verifier
return verifier
}
func (github *GithubOAuthService) GetAuthURL(state string) string { func (github *GithubOAuthService) GetAuthURL(state string) string {
return github.config.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(github.verifier)) return github.config.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(github.verifier))
} }

View File

@@ -48,10 +48,7 @@ func (google *GoogleOAuthService) Init() error {
httpClient := &http.Client{} httpClient := &http.Client{}
ctx := context.Background() ctx := context.Background()
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient) ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
verifier := oauth2.GenerateVerifier()
google.context = ctx google.context = ctx
google.verifier = verifier
return nil return nil
} }
@@ -65,6 +62,12 @@ func (oauth *GoogleOAuthService) GenerateState() string {
return state return state
} }
func (google *GoogleOAuthService) GenerateVerifier() string {
verifier := oauth2.GenerateVerifier()
google.verifier = verifier
return verifier
}
func (google *GoogleOAuthService) GetAuthURL(state string) string { func (google *GoogleOAuthService) GetAuthURL(state string) string {
return google.config.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(google.verifier)) return google.config.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(google.verifier))
} }

View File

@@ -11,6 +11,7 @@ import (
type OAuthService interface { type OAuthService interface {
Init() error Init() error
GenerateState() string GenerateState() string
GenerateVerifier() string
GetAuthURL(state string) string GetAuthURL(state string) string
VerifyCode(code string) error VerifyCode(code string) error
Userinfo() (config.Claims, error) Userinfo() (config.Claims, error)