From e34a68b91a9d928ccbcd43e5784a9b70e99d9e34 Mon Sep 17 00:00:00 2001 From: cubicroot Date: Wed, 25 Dec 2024 19:01:36 +0100 Subject: [PATCH] allow usage of API token instead of session --- README.md | 5 +++-- cmd/main.go | 1 + internal/c3hub/schedule.go | 9 +++------ internal/c3hub/service.go | 17 ++++++++++++++++- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3322e08..def2634 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,10 @@ See "config" section on how to setup, run the `cmd/main.go` and call `{LISTEN_AD Set the following environment variables: * `HUB_API_BASE_URL`: Base URL of the hubs API - usually is mentioned in the [hub repo](https://git.cccv.de/hub/hub) -* `HUB_API_SESSION`: Session token from your user account at the hub (the value of the session cookie) +* `HUB_API_TOKEN`: API token for the hubs API - see [hub repo](https://git.cccv.de/hub/hub) for instructions to obtain it * `TOKEN`: the token required to query the iCal file * `LISTEN_ADDR`: address the webserver will listen on - most likely `127.0.0.1:80` or `0.0.0.0:80` +* `HUB_API_SESSION`: Session token from your user account at the hub (the value of the session cookie); NOT RECOMMENDED, use API token instead! ## Docker Compose @@ -34,7 +35,7 @@ services: user: "0:0" environment: - 'HUB_API_BASE_URL=https://example.com/congress/2024' - - 'HUB_API_SESSION=xxx' + - 'HUB_API_TOKEN=xxx' - 'TOKEN=yyy' # Take care to expose this port in some way. - 'LISTEN_ADDR=0.0.0.0:8000' diff --git a/cmd/main.go b/cmd/main.go index 30c6b6c..54ab017 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -11,6 +11,7 @@ func main() { hub := c3hub.New(c3hub.Config{ BaseURL: os.Getenv("HUB_API_BASE_URL"), SessionCookie: os.Getenv("HUB_API_SESSION"), + APIToken: os.Getenv("HUB_API_TOKEN"), }) server := api.New(api.Config{ diff --git a/internal/c3hub/schedule.go b/internal/c3hub/schedule.go index e6e76d0..a17e70e 100644 --- a/internal/c3hub/schedule.go +++ b/internal/c3hub/schedule.go @@ -15,8 +15,7 @@ func (service *service) GetMySchedule( if err != nil { return nil, err } - r.Header.Add("Cookie", "38C3_SESSION="+service.config.SessionCookie) - r = r.WithContext(ctx) + r = service.injectAuth(ctx, r) resp, err := http.DefaultClient.Do(r) if err != nil { @@ -69,8 +68,7 @@ func (service *service) getEvents( if err != nil { return nil, err } - r.Header.Add("Cookie", "38C3_SESSION="+service.config.SessionCookie) - r = r.WithContext(ctx) + r = service.injectAuth(ctx, r) resp, err := http.DefaultClient.Do(r) if err != nil { @@ -144,8 +142,7 @@ func (service *service) getRooms( if err != nil { return nil, err } - r.Header.Add("Cookie", "38C3_SESSION="+service.config.SessionCookie) - r = r.WithContext(ctx) + r = service.injectAuth(ctx, r) resp, err := http.DefaultClient.Do(r) if err != nil { diff --git a/internal/c3hub/service.go b/internal/c3hub/service.go index ebf8474..5c61479 100644 --- a/internal/c3hub/service.go +++ b/internal/c3hub/service.go @@ -1,12 +1,17 @@ package c3hub -import "time" +import ( + "context" + "net/http" + "time" +) type Config struct { BaseURL string Day1 time.Time SessionCookie string + APIToken string } type service struct { @@ -18,3 +23,13 @@ func New(conf Config) Service { config: conf, } } + +func (service *service) injectAuth(ctx context.Context, r *http.Request) *http.Request { + if service.config.APIToken != "" { + r.Header.Add("Authorization", "Token "+service.config.APIToken) + } else { + r.Header.Add("Cookie", "38C3_SESSION="+service.config.SessionCookie) + } + + return r.WithContext(ctx) +}