Skip to content

Commit

Permalink
allow usage of API token instead of session
Browse files Browse the repository at this point in the history
  • Loading branch information
CubicrootXYZ committed Dec 25, 2024
1 parent f37bf79 commit e34a68b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'
Expand Down
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
9 changes: 3 additions & 6 deletions internal/c3hub/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 16 additions & 1 deletion internal/c3hub/service.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
}

0 comments on commit e34a68b

Please sign in to comment.