Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up OAuth config #430

Merged
merged 2 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ else
LDFLAGS = -ldflags="-X 'tidbyt.dev/pixlet/cmd.Version=$(GIT_COMMIT)'"
endif

all: build

test:
go test -v -cover ./...

Expand All @@ -19,7 +21,7 @@ clean:
bench:
go test -benchmem -benchtime=20s -bench BenchmarkRunAndRender tidbyt.dev/pixlet/encode

build: clean
build:
go build $(LDFLAGS) -o $(BINARY) tidbyt.dev/pixlet

embedfonts:
Expand Down
14 changes: 14 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,22 @@ import (
"golang.org/x/oauth2"
)

const (
oauthCallbackAddr = "localhost:8085"
)

var (
privateConfig = viper.New()

oauthConf = &oauth2.Config{
ClientID: "d8ae7ea0-4a1a-46b0-b556-6d742687223a",
Scopes: []string{"device", "offline_access"},
Endpoint: oauth2.Endpoint{
AuthURL: "https://login.tidbyt.com/oauth2/auth",
TokenURL: "https://login.tidbyt.com/oauth2/token",
},
RedirectURL: fmt.Sprintf("http://%s", oauthCallbackAddr),
}
)

func init() {
Expand Down
41 changes: 5 additions & 36 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,26 @@ import (
cv "github.com/nirasan/go-oauth-pkce-code-verifier"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"golang.org/x/oauth2"
"golang.org/x/oauth2/authhandler"
)

const (
oauthClientID = "d8ae7ea0-4a1a-46b0-b556-6d742687223a"
oauthCallbackAddr = "localhost:8085"

oauthBaseURL = "https://login.tidbyt.com/oauth2/auth"
oauthTokenURL = "https://login.tidbyt.com/oauth2/token"
)

var LoginCmd = &cobra.Command{
Use: "login",
Short: "Login to your Tidbyt account",
Example: "login",
Run: login,
}

var (
oauthConf = &oauth2.Config{
ClientID: "d8ae7ea0-4a1a-46b0-b556-6d742687223a",
Scopes: []string{"device", "offline_access"},
Endpoint: oauth2.Endpoint{
AuthURL: "https://login.tidbyt.com/oauth2/auth",
TokenURL: "https://login.tidbyt.com/oauth2/token",
},
RedirectURL: fmt.Sprintf("http://%s", oauthCallbackAddr),
}
)

type authResult struct {
code string
state string
err error
}

func login(cmd *cobra.Command, args []string) {
server := &http.Server{
Addr: oauthCallbackAddr,
}

authResult := struct {
code string
state string
}{}

done := make(chan bool, 1)
var authCode, authState string
done := make(chan bool)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
authResult.code = r.URL.Query().Get("code")
authResult.state = r.URL.Query().Get("state")
authCode = r.URL.Query().Get("code")
authState = r.URL.Query().Get("state")

w.Header().Set("content-type", "text/plain")
io.WriteString(w, "please close this window and return to pixlet")
Expand All @@ -84,7 +53,7 @@ func login(cmd *cobra.Command, args []string) {
go server.ListenAndServe()
<-done

return authResult.code, authResult.state, nil
return authCode, authState, nil
}

cv, err := cv.CreateCodeVerifier()
Expand Down