Skip to content

Commit

Permalink
feat: add api_token to livestream jwt claim
Browse files Browse the repository at this point in the history
this allows us to drop the dependency on postgres
  • Loading branch information
frankh committed Jun 19, 2024
1 parent 89310a8 commit 51f91ce
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 72 deletions.
6 changes: 6 additions & 0 deletions livestream/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strings"

"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
Expand All @@ -23,4 +24,9 @@ func loadConfigs() {
fmt.Println("Config file changed:", e.Name)
})
viper.WatchConfig()

viper.SetEnvPrefix("livestream") // will be uppercased automatically
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.BindEnv("jwt.secret") // read from LIVESTREAM_JWT_SECRET
}
18 changes: 0 additions & 18 deletions livestream/db.go

This file was deleted.

2 changes: 1 addition & 1 deletion livestream/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func decodeAuthToken(authHeader string) (jwt.MapClaims, error) {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
// Here you should specify the secret used to sign your JWTs.
return []byte(viper.GetString("jwt.token")), nil
return []byte(viper.GetString("jwt.secret")), nil
})

if err != nil {
Expand Down
22 changes: 3 additions & 19 deletions livestream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strconv"
Expand Down Expand Up @@ -104,12 +105,7 @@ func main() {
if err != nil {
return err
}
teamIdInt := int(claims["team_id"].(float64))

token, err := tokenFromTeamId(teamIdInt)
if err != nil {
return err
}
token := fmt.Sprint(claims["api_token"])

var hash *expirable.LRU[string, string]
var ok bool
Expand Down Expand Up @@ -155,26 +151,14 @@ func main() {
return err
}
teamId = strconv.Itoa(int(claims["team_id"].(float64)))
token = fmt.Sprint(claims["api_token"])

log.Printf("~~~~ team found %s", teamId)
if teamId == "" {
return errors.New("teamId is required unless geo=true")
}
}

if teamId != "" {
teamIdInt64, err := strconv.ParseInt(teamId, 10, 0)
if err != nil {
return err
}

teamIdInt := int(teamIdInt64)
token, err = tokenFromTeamId(teamIdInt)
if err != nil {
return err
}
}

eventTypes := []string{}
if eventType != "" {
eventTypes = strings.Split(eventType, ",")
Expand Down
33 changes: 0 additions & 33 deletions livestream/posthog.go

This file was deleted.

2 changes: 1 addition & 1 deletion posthog/api/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def get_groups_on_events_querying_enabled(self, team: Team) -> bool:

def get_live_events_token(self, team: Team) -> Optional[str]:
return encode_jwt(
{"team_id": team.id},
{"team_id": team.id, "api_token": team.api_token},
timedelta(days=7),
PosthogJwtAudience.LIVESTREAM,
)
Expand Down

0 comments on commit 51f91ce

Please sign in to comment.