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 2 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
28 changes: 24 additions & 4 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 @@ -215,9 +235,9 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: aURL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed,PTAL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, ignore the ptal, I see you pre-approved, thank you!

fmt.Printf("Authorize me at following URL, please:\n\n%s\n\nCode: ", aurl)
var code string
if _, err := fmt.Scan(&code); err != nil {
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