diff options
| author | Leonardo Bishop <me@leonardobishop.net> | 2025-07-14 01:24:40 +0100 |
|---|---|---|
| committer | Leonardo Bishop <me@leonardobishop.net> | 2025-07-14 01:24:40 +0100 |
| commit | 08a3fb8a2b0281c3c329b33215ec7f8866add606 (patch) | |
| tree | ff8a5413449ea198bc063bf0099fc025ea49c82b /pkg | |
| parent | 684787bcb72aece2aa914597a3bc8788432e66f7 (diff) | |
Add authentication and ability to change host
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/auth/provider.go | 38 | ||||
| -rw-r--r-- | pkg/index/index.go | 11 | ||||
| -rw-r--r-- | pkg/site/site.go | 6 |
3 files changed, 54 insertions, 1 deletions
diff --git a/pkg/auth/provider.go b/pkg/auth/provider.go new file mode 100644 index 0000000..0d515ab --- /dev/null +++ b/pkg/auth/provider.go @@ -0,0 +1,38 @@ +package auth + +import ( + "time" + + "github.com/golang-jwt/jwt/v5" +) + +type Authenticator struct { + secretKey []byte + parser *jwt.Parser +} + +func NewAuthenticator(secretKey []byte) *Authenticator { + parser := jwt.NewParser(jwt.WithIssuer("scrapbook"), jwt.WithExpirationRequired()) + + a := &Authenticator{ + secretKey: secretKey, + parser: parser, + } + + return a +} + +func (a *Authenticator) NewJwt() (string, error) { + t := jwt.NewWithClaims(jwt.SigningMethodHS256, + jwt.MapClaims{ + "iss": "scrapbook", + "exp": jwt.NewNumericDate(time.Now().Add(time.Hour * 2)), + }) + + return t.SignedString(a.secretKey) +} + +func (a *Authenticator) VerifyJwt(token string) error { + _, err := a.parser.Parse(token, func(t *jwt.Token) (interface{}, error) { return a.secretKey, nil }) + return err +} diff --git a/pkg/index/index.go b/pkg/index/index.go index 35423bd..f3dc00d 100644 --- a/pkg/index/index.go +++ b/pkg/index/index.go @@ -55,9 +55,18 @@ func (s *SiteIndex) AddSite(site *site.Site) { s.updateSiteIndexes() } +func (s *SiteIndex) UpdateSiteIndexes() { + s.mu.Lock() + defer s.mu.Unlock() + + s.updateSiteIndexes() +} + func (s *SiteIndex) updateSiteIndexes() { clear(s.sitesByHost) for _, site := range s.sites { - s.sitesByHost[site.SiteConfig.Host] = site + if site.SiteConfig.Host != "" { + s.sitesByHost[site.SiteConfig.Host] = site + } } } diff --git a/pkg/site/site.go b/pkg/site/site.go index 3daf5e7..704c34d 100644 --- a/pkg/site/site.go +++ b/pkg/site/site.go @@ -133,6 +133,9 @@ func (s *Site) CreateNewVersion() (string, error) { } func (s *Site) EvaluateSiteStatus() string { + if s.SiteConfig.Host == "" { + return "inactive" + } stat, err := os.Stat(s.GetCurrentPath()) if err != nil || !stat.IsDir() { return "inactive" @@ -145,6 +148,9 @@ func (s *Site) EvaluateSiteStatus() string { } func (s *Site) EvaluateSiteStatusReason() string { + if s.SiteConfig.Host == "" { + return "This site is not served by scrapbook" + } stat, err := os.Stat(s.GetCurrentPath()) if err != nil || !stat.IsDir() { return "This site is inacessible because no version is active" |
