Skip to content

Commit

Permalink
Merge pull request #8 from rneatherway/rneatherway/token-env-var
Browse files Browse the repository at this point in the history
Read authn information from environment for now
  • Loading branch information
rneatherway authored Apr 26, 2022
2 parents bfdadb7 + 9076824 commit 31514f2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.17.1
go-version: 1.18.1
- name: Build
uses: goreleaser/goreleaser-action@v2
with:
Expand All @@ -34,6 +34,6 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.17.1
go-version: 1.18.1
- name: Test
run: go test ./...
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.17.1
go-version: 1.18.1
- name: Release
uses: goreleaser/goreleaser-action@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/rneatherway/gh-slack

go 1.17
go 1.18

require (
github.com/cli/go-gh v0.0.3
Expand Down
2 changes: 1 addition & 1 deletion internal/slackclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func New(team string, log *log.Logger) (*SlackClient, error) {
}
cachePath := path.Join(dataHome, "gh-slack")

auth, err := getSlackAuth(team)
auth, err := getSlackAuthFromEnv()
if err != nil {
return nil, err
}
Expand Down
29 changes: 29 additions & 0 deletions internal/slackclient/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"regexp"
"runtime"
"strings"

_ "modernc.org/sqlite"
)
Expand Down Expand Up @@ -88,3 +90,30 @@ func getSlackAuth(team string) (*SlackAuth, error) {

return &SlackAuth{Token: string(matches[1]), Cookies: map[string]string{"d": cookie}}, nil
}

func getSlackAuthFromEnv() (*SlackAuth, error) {
slackAuth := os.Getenv("SLACK_AUTH")
if slackAuth == "" {
return nil, errors.New("environment variable SLACK_AUTH not set")
}

token, cookie, found := strings.Cut(slackAuth, "\n")
if !found {
return nil, errors.New("environment variable SLACK_AUTH not in expected format")
}

key, value, found := strings.Cut(cookie, "=")
if !found {
return nil, errors.New("environment variable SLACK_AUTH not in expected format")
}

cookie, err := url.PathUnescape(value)
if err != nil {
return nil, fmt.Errorf("failed to unescape cookie value: %w", err)
}

return &SlackAuth{
Token: token,
Cookies: map[string]string{key: cookie},
}, nil
}

0 comments on commit 31514f2

Please sign in to comment.