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

fix oob oauth to use localhost and update elements bucket #798

Merged
merged 3 commits into from
Nov 9, 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
30 changes: 25 additions & 5 deletions claat/fetch/drive/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path"
Expand All @@ -43,14 +44,33 @@ var (
ClientID: googClient,
ClientSecret: googSecret,
Scopes: []string{scopeDriveReadOnly},
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
RedirectURL: "http://localhost:8091",
Endpoint: oauth2.Endpoint{
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
},
}
)

// The webserver waits for an oauth code in the three-legged auth flow.
func startWebServer() (code string, err error) {
listener, err := net.Listen("tcp", "localhost:8091")
if err != nil {
return "", err
}
codeCh := make(chan string)

go http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
code := r.FormValue("code")
codeCh <- code // send code to OAuth flow
listener.Close()
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Received oauth code\r\nYou can now safely close this browser window.")
}))
code = <- codeCh
return code, nil
}

type authorizationHandler func(conf *oauth2.Config) (*oauth2.Token, error)

type internalOptions struct {
Expand Down Expand Up @@ -214,10 +234,10 @@ func (c *cachedTokenSource) Token() (*oauth2.Token, error) {

// authorize performs user authorization flow, asking for permissions grant.
func authorize(conf *oauth2.Config) (*oauth2.Token, error) {
aurl := conf.AuthCodeURL("unused", oauth2.AccessTypeOffline)
fmt.Printf("Authorize me at following URL, please:\n\n%s\n\nCode: ", aurl)
var code string
if _, err := fmt.Scan(&code); err != nil {
aURL := conf.AuthCodeURL("unused", oauth2.AccessTypeOffline)
fmt.Printf("Authorize me at following URL, please:\n\n%s\n", aURL)
code, err := startWebServer()
if err != nil {
return nil, err
}
return conf.Exchange(context.Background(), code)
Expand Down
10 changes: 5 additions & 5 deletions claat/render/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<title>{{.Meta.Title}}</title>
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Source+Code+Pro:400|Roboto:400,300,400italic,500,700|Roboto+Mono">
<link rel="stylesheet" href="//fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="{{.Prefix}}/codelab-elements/codelab-elements.css">
<link rel="stylesheet" href="{{.Prefix}}/claat-public/codelab-elements.css">
<style>
.success {
color: #1e8e3e;
Expand All @@ -47,10 +47,10 @@
{{end}}{{end}}
</google-codelab>

<script src="{{.Prefix}}/codelab-elements/native-shim.js"></script>
<script src="{{.Prefix}}/codelab-elements/custom-elements.min.js"></script>
<script src="{{.Prefix}}/codelab-elements/prettify.js"></script>
<script src="{{.Prefix}}/codelab-elements/codelab-elements.js"></script>
<script src="{{.Prefix}}/claat-public/native-shim.js"></script>
<script src="{{.Prefix}}/claat-public/custom-elements.min.js"></script>
<script src="{{.Prefix}}/claat-public/prettify.js"></script>
<script src="{{.Prefix}}/claat-public/codelab-elements.js"></script>
<script src="//support.google.com/inapp/api.js"></script>

</body>
Expand Down