Skip to content

Commit

Permalink
[preprocessor/webhook] reuse browsertrix jwt before exp
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Jun 18, 2024
1 parent 32a6628 commit 4316301
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion webhook/browsertrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/starlinglab/integrity-v2/aa"
"github.com/starlinglab/integrity-v2/config"
wacz "github.com/starlinglab/integrity-v2/preprocessor/wacz"
Expand Down Expand Up @@ -41,7 +43,19 @@ type LoginResponse struct {
TokenType string `json:"token_type"`
}

var browsertrixJwtMutex = &sync.RWMutex{}
var browsertrixJwtToken string

func getJWTToken() (string, error) {
browsertrixJwtMutex.RLock()
if browsertrixJwtToken != "" {
token, err := jwt.Parse([]byte(browsertrixJwtToken), nil)
browsertrixJwtMutex.RUnlock()
if err == nil && token.Expiration().After(time.Now()) {
return browsertrixJwtToken, nil
}
}
browsertrixJwtMutex.Lock()
b, err := json.Marshal(map[string]string{
"username": config.GetConfig().Browsertrix.User,
"password": config.GetConfig().Browsertrix.Password,
Expand Down Expand Up @@ -70,7 +84,9 @@ func getJWTToken() (string, error) {
if err != nil {
return "", err
}
return value.AccessToken, nil
browsertrixJwtToken = value.AccessToken
browsertrixJwtMutex.Unlock()
return browsertrixJwtToken, nil
}

type CrawlInfoResponse struct {
Expand Down

0 comments on commit 4316301

Please sign in to comment.