From e650bde5a5a52695f34fe1f6d6d27e4cb7a34567 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 5 Sep 2023 21:01:24 +0200 Subject: [PATCH] added option to use proxy for oauth --- cmd/morningalarm.go | 4 ++++ internal/morningalarm.go | 8 +++++++- internal/spotify.go | 26 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/cmd/morningalarm.go b/cmd/morningalarm.go index 5dda4bd..924f1b7 100644 --- a/cmd/morningalarm.go +++ b/cmd/morningalarm.go @@ -12,6 +12,8 @@ type args struct { PlaylistID string `arg:"--playlist-id,required,env:SPOTIFY_PLAYLIST_ID" help:"Just the id" placeholder:"SPOTIFY_PLAYLIST_ID"` RedirectURL string `arg:"--redirect-url,required,env:SPOTIFY_REDIRECT_URL" help:"Must be the same as in the Spotify developer dashboard." placeholder:"SPOTIFY_REDIRECT_URL"` ListenAddr string `arg:"--listen-addr,env:LISTEN_ADDR" help:"Address to listen on" placeholder:"LISTEN_ADDR" default:":3000"` + OAuthProxyURL string `arg:"--oauth-proxy-url,env:OAUTH_PROXY_URL" help:"URL of the oauth proxy" placeholder:"OAUTH_PROXY_URL"` + OAuthProxyAPIKey string `arg:"--oauth-proxy-api-key,env:OAUTH_PROXY_API_KEY" help:"API key for the oauth proxy" placeholder:"OAUTH_PROXY_API_KEY"` } func main() { @@ -25,5 +27,7 @@ func main() { PlaylistID: args.PlaylistID, RedirectURL: args.RedirectURL, ListenAddr: args.ListenAddr, + OAuthProxyURL: args.OAuthProxyURL, + OAuthProxyAPIKey: args.OAuthProxyAPIKey, }).Start() } diff --git a/internal/morningalarm.go b/internal/morningalarm.go index 2d842d3..0769c0d 100644 --- a/internal/morningalarm.go +++ b/internal/morningalarm.go @@ -19,6 +19,8 @@ type MorningAlarmConfig struct { PlaylistID string `json:"wakeupContext"` RedirectURL string `json:"redirectUrl"` ListenAddr string `json:"listenAddr"` + OAuthProxyURL string `json:"oauthProxyUrl"` + OAuthProxyAPIKey string `json:"oauthProxyApiKey"` } type MorningAlarm struct { @@ -42,7 +44,11 @@ func (ma *MorningAlarm) Start() { ma.ro = gin.Default() - ma.setupSpotify() + if ma.config.OAuthProxyURL != "" { + ma.setupSpotifyWithProxy() + } else { + ma.setupSpotify() + } ma.setupWebserver() diff --git a/internal/spotify.go b/internal/spotify.go index 092d200..3756ba1 100644 --- a/internal/spotify.go +++ b/internal/spotify.go @@ -16,6 +16,32 @@ import ( const redirectURI = "/api/spotifycb" +type headerTransport struct { + baseTransport http.RoundTripper + headers map[string]string +} + +func (t *headerTransport) RoundTrip(req *http.Request) (*http.Response, error) { + for key, value := range t.headers { + req.Header.Set(key, value) + } + + return t.baseTransport.RoundTrip(req) +} + +func (ma *MorningAlarm) setupSpotifyWithProxy() { + client := &http.Client{ + Transport: &headerTransport{ + baseTransport: http.DefaultTransport, + headers: map[string]string{ + "X-API-KEY": ma.config.OAuthProxyAPIKey, + }, + }, + } + + ma.sp = spotify.New(client, spotify.WithBaseURL(ma.config.OAuthProxyURL)) +} + func (ma *MorningAlarm) setupSpotify() error { auth := spotifyauth.New( spotifyauth.WithRedirectURL(ma.config.RedirectURL+redirectURI),