summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-10-18 11:28:43 +0300
committerPaul Buetow <paul@buetow.org>2024-10-18 11:28:43 +0300
commit9525b8c9a3f714235c8e591f3eddd4c05ed791da (patch)
treee0df3658d353649a7a23d0109b3f6924bba10af9
parent56b272ca1b678023c51d730acda04e56867ad141 (diff)
use context in oauth2
-rw-r--r--internal/oi/oi.go1
-rw-r--r--internal/platforms/linkedin/linkedin.go3
-rw-r--r--internal/platforms/linkedin/oauth2/oauth2.go7
3 files changed, 6 insertions, 5 deletions
diff --git a/internal/oi/oi.go b/internal/oi/oi.go
index 1716082..1f7de6d 100644
--- a/internal/oi/oi.go
+++ b/internal/oi/oi.go
@@ -48,7 +48,6 @@ func ReadDirCh[T any](dir string, cb func(file os.DirEntry) (T, bool)) (chan T,
return ch, nil
}
-// TODO: Refactor to use ReadDirCh internally
func TraverseDir(dir string, cb func(file os.DirEntry) error) error {
if err := EnsureDir(dir); err != nil {
return err
diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go
index 2e7e3ca..341834e 100644
--- a/internal/platforms/linkedin/linkedin.go
+++ b/internal/platforms/linkedin/linkedin.go
@@ -33,7 +33,7 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry)
log.Println("Not posting", ent, "to LinkedIn as dry-run enabled")
return nil
}
- personID, accessToken, err := oauth2.LinkedInCreds(args)
+ personID, accessToken, err := oauth2.LinkedInCreds(ctx, args)
if err != err {
return err
}
@@ -78,6 +78,7 @@ func callLinkedInAPI(personID, accessToken, message string) error {
req.Header.Add("X-RestLi-Protocol-Version", "2.0.0")
client := &http.Client{}
+ // TODO: Use ctx?
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("Error sending request: %w", err)
diff --git a/internal/platforms/linkedin/oauth2/oauth2.go b/internal/platforms/linkedin/oauth2/oauth2.go
index 21caf92..8c03761 100644
--- a/internal/platforms/linkedin/oauth2/oauth2.go
+++ b/internal/platforms/linkedin/oauth2/oauth2.go
@@ -22,6 +22,7 @@ var (
oauthAccessToken string
oauthPersonID string
errCh chan error
+ globalCtx context.Context
)
func getOauthPersonID(token *oauth2.Token) (string, error) {
@@ -72,8 +73,7 @@ func oauthCallbackHandler(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
log.Println("Exchanging OAuth2 token")
- // TODO: Insert the propper context
- token, err := oauthConfig.Exchange(context.Background(), code)
+ token, err := oauthConfig.Exchange(globalCtx, code)
if err != nil {
_, _ = w.Write([]byte(err.Error()))
errCh <- err
@@ -90,7 +90,7 @@ func oauthCallbackHandler(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Successfully fetched LinkedIn person ID\n"))
}
-func LinkedInCreds(args config.Args) (string, string, error) {
+func LinkedInCreds(ctx context.Context, args config.Args) (string, string, error) {
secrets := args.Secrets
if secrets.LinkedInAccessToken != "" && secrets.LinkedInPersonID != "" {
return secrets.LinkedInPersonID, secrets.LinkedInAccessToken, nil
@@ -104,6 +104,7 @@ func LinkedInCreds(args config.Args) (string, string, error) {
Endpoint: linkedin.Endpoint,
}
errCh = make(chan error)
+ globalCtx = ctx
http.HandleFunc("/", oauthIndexHandler)
http.HandleFunc("/callback", oauthCallbackHandler)