Skip to content

Commit

Permalink
add context to claims
Browse files Browse the repository at this point in the history
  • Loading branch information
vintikzzz committed Nov 30, 2024
1 parent c24c34d commit 4770c60
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion handlers/embed/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *Handler) post(c *gin.Context) {
tpl.HTMLWithErr(err, http.StatusBadRequest, c, pd)
return
}
dsd, err := s.ds.Get(u.Hostname())
dsd, err := s.ds.Get(c.Request.Context(), u.Hostname())
if err != nil {
tpl.HTMLWithErr(err, http.StatusBadRequest, c, pd)
return
Expand Down
10 changes: 4 additions & 6 deletions services/claims/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,22 @@ func New(c *cli.Context, cl *Client) *Claims {
}
}

func (s *Claims) get(email string) (resp *Data, err error) {
func (s *Claims) get(ctx context.Context, email string) (resp *Data, err error) {
var cl proto.ClaimsProviderClient
cl, err = s.cl.Get()
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
resp, err = cl.Get(ctx, &proto.GetRequest{Email: email})
if err != nil {
return nil, errors.WithMessage(err, "failed to get claims")
}
return
}

func (s *Claims) Get(email string) (*Data, error) {
func (s *Claims) Get(ctx context.Context, email string) (*Data, error) {
resp, err := s.LazyMap.Get(email, func() (interface{}, error) {
return s.get(email)
return s.get(ctx, email)
})
if err != nil {
return nil, err
Expand All @@ -75,7 +73,7 @@ func (s *Claims) Get(email string) (*Data, error) {

func (s *Claims) MakeUserClaimsFromContext(c *gin.Context) (*Data, error) {
u := auth.GetUserFromContext(c)
r, err := s.Get(u.Email)
r, err := s.Get(c.Request.Context(), u.Email)
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions services/embed/domain_settings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package embed

import (
"context"
"github.com/pkg/errors"
"time"

Expand Down Expand Up @@ -31,7 +32,7 @@ func NewDomainSettings(pg *cs.PG, claims *claims.Claims) *DomainSettings {
}
}

func (s *DomainSettings) get(domain string) (*DomainSettingsData, error) {
func (s *DomainSettings) get(ctx context.Context, domain string) (*DomainSettingsData, error) {
if s.pg == nil || s.pg.Get() == nil || s.claims == nil {
return &DomainSettingsData{}, nil
}
Expand All @@ -43,16 +44,16 @@ func (s *DomainSettings) get(domain string) (*DomainSettingsData, error) {
} else if err != nil {
return nil, err
}
cl, err := s.claims.Get(em.Email)
cl, err := s.claims.Get(ctx, em.Email)
if err != nil {
return nil, err
}
return &DomainSettingsData{Ads: em.Ads || !cl.Claims.Embed.NoAds}, nil
}

func (s *DomainSettings) Get(domain string) (*DomainSettingsData, error) {
func (s *DomainSettings) Get(ctx context.Context, domain string) (*DomainSettingsData, error) {
resp, err := s.LazyMap.Get(domain, func() (interface{}, error) {
return s.get(domain)
return s.get(ctx, domain)
})
if err != nil {
return nil, err
Expand Down

0 comments on commit 4770c60

Please sign in to comment.