mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-07-09 03:30:18 +00:00
chore: review nitpicks
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -20,21 +21,21 @@ type GithubUserinfoResponse struct {
|
|||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultExtractor(client *http.Client, url string) (*model.Claims, error) {
|
func defaultExtractor(client *http.Client, ctx context.Context, url string) (*model.Claims, error) {
|
||||||
return simpleReq[model.Claims](client, url, nil)
|
return simpleReq[model.Claims](client, ctx, url, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func githubExtractor(client *http.Client, _ string) (*model.Claims, error) {
|
func githubExtractor(client *http.Client, ctx context.Context, _ string) (*model.Claims, error) {
|
||||||
var user model.Claims
|
var user model.Claims
|
||||||
|
|
||||||
userInfo, err := simpleReq[GithubUserinfoResponse](client, "https://api.github.com/user", map[string]string{
|
userInfo, err := simpleReq[GithubUserinfoResponse](client, ctx, "https://api.github.com/user", map[string]string{
|
||||||
"accept": "application/vnd.github+json",
|
"accept": "application/vnd.github+json",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
userEmails, err := simpleReq[GithubEmailResponse](client, "https://api.github.com/user/emails", map[string]string{
|
userEmails, err := simpleReq[GithubEmailResponse](client, ctx, "https://api.github.com/user/emails", map[string]string{
|
||||||
"accept": "application/vnd.github+json",
|
"accept": "application/vnd.github+json",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OAuthUserinfoExtractor func(client *http.Client, url string) (*model.Claims, error)
|
type OAuthUserinfoExtractor func(client *http.Client, ctx context.Context, url string) (*model.Claims, error)
|
||||||
|
|
||||||
type OAuthService struct {
|
type OAuthService struct {
|
||||||
serviceCfg model.OAuthServiceConfig
|
serviceCfg model.OAuthServiceConfig
|
||||||
@@ -80,7 +80,7 @@ func (s *OAuthService) GetToken(code string, verifier string) (*oauth2.Token, er
|
|||||||
|
|
||||||
func (s *OAuthService) GetUserinfo(token *oauth2.Token) (*model.Claims, error) {
|
func (s *OAuthService) GetUserinfo(token *oauth2.Token) (*model.Claims, error) {
|
||||||
client := oauth2.NewClient(s.ctx, oauth2.StaticTokenSource(token))
|
client := oauth2.NewClient(s.ctx, oauth2.StaticTokenSource(token))
|
||||||
return s.userinfoExtractor(client, s.serviceCfg.UserinfoURL)
|
return s.userinfoExtractor(client, s.ctx, s.serviceCfg.UserinfoURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *OAuthService) GetConfig() model.OAuthServiceConfig {
|
func (s *OAuthService) GetConfig() model.OAuthServiceConfig {
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func simpleReq[T any](client *http.Client, url string, headers map[string]string) (*T, error) {
|
func simpleReq[T any](client *http.Client, ctx context.Context, url string, headers map[string]string) (*T, error) {
|
||||||
var decodedRes T
|
var decodedRes T
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -26,7 +27,11 @@ func simpleReq[T any](client *http.Client, url string, headers map[string]string
|
|||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||||
return nil, fmt.Errorf("request failed with status: %s", res.Status)
|
body, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("request failed with status: %s", res.Status)
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("request failed with status: %s and body: %s", res.Status, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(res.Body)
|
body, err := io.ReadAll(res.Body)
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ type TailscaleService struct {
|
|||||||
config *model.Config
|
config *model.Config
|
||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
client *http.Client
|
client *http.Client
|
||||||
|
ctx context.Context
|
||||||
|
|
||||||
caches struct {
|
caches struct {
|
||||||
devices *CacheStore[tailscaleAPIDevices]
|
devices *CacheStore[tailscaleAPIDevices]
|
||||||
@@ -71,6 +72,7 @@ type TailscaleServiceInput struct {
|
|||||||
Ding *ding.Ding
|
Ding *ding.Ding
|
||||||
Config *model.Config
|
Config *model.Config
|
||||||
Log *logger.Logger
|
Log *logger.Logger
|
||||||
|
Ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTailscaleService(i TailscaleServiceInput) (*TailscaleService, error) {
|
func NewTailscaleService(i TailscaleServiceInput) (*TailscaleService, error) {
|
||||||
@@ -89,6 +91,7 @@ func NewTailscaleService(i TailscaleServiceInput) (*TailscaleService, error) {
|
|||||||
s := &TailscaleService{
|
s := &TailscaleService{
|
||||||
config: i.Config,
|
config: i.Config,
|
||||||
log: i.Log,
|
log: i.Log,
|
||||||
|
ctx: i.Ctx,
|
||||||
}
|
}
|
||||||
|
|
||||||
devicesCache := NewCacheStore[tailscaleAPIDevices](0)
|
devicesCache := NewCacheStore[tailscaleAPIDevices](0)
|
||||||
@@ -114,7 +117,9 @@ func NewTailscaleService(i TailscaleServiceInput) (*TailscaleService, error) {
|
|||||||
|
|
||||||
s.urls.devices = tailscaleAPIDeviceList(i.Config.Tailscale.Tailnet)
|
s.urls.devices = tailscaleAPIDeviceList(i.Config.Tailscale.Tailnet)
|
||||||
s.urls.users = tailscaleAPIUserList(i.Config.Tailscale.Tailnet)
|
s.urls.users = tailscaleAPIUserList(i.Config.Tailscale.Tailnet)
|
||||||
s.client = &http.Client{}
|
s.client = &http.Client{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
_, _, err := s.getDeviceList()
|
_, _, err := s.getDeviceList()
|
||||||
|
|
||||||
@@ -144,7 +149,7 @@ func (s *TailscaleService) getDeviceList() (*tailscaleAPIDevices, bool, error) {
|
|||||||
return &cached, true, nil
|
return &cached, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
devices, err := simpleReq[tailscaleAPIDevices](s.client, s.urls.devices, map[string]string{
|
devices, err := simpleReq[tailscaleAPIDevices](s.client, s.ctx, s.urls.devices, map[string]string{
|
||||||
"Authorization": s.buildAuthorizationHeader(),
|
"Authorization": s.buildAuthorizationHeader(),
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -164,7 +169,7 @@ func (s *TailscaleService) getUsersList() (*tailscaleAPIUsers, bool, error) {
|
|||||||
return &cached, true, nil
|
return &cached, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
users, err := simpleReq[tailscaleAPIUsers](s.client, s.urls.users, map[string]string{
|
users, err := simpleReq[tailscaleAPIUsers](s.client, s.ctx, s.urls.users, map[string]string{
|
||||||
"Authorization": s.buildAuthorizationHeader(),
|
"Authorization": s.buildAuthorizationHeader(),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user