-
Notifications
You must be signed in to change notification settings - Fork 7
docs(api-clients): add OAuth authentication examples for all SDK languages #245
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
Open
steve-calvert-glean
wants to merge
3
commits into
main
Choose a base branch
from
docs/oauth-authentication-examples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
4e2b392
docs(api-clients): add OAuth authentication examples for all SDK lang…
steve-calvert-glean 0ebbdfc
fix(oauth-docs): correct OAuth examples with proper security practices
steve-calvert-glean 98d1385
fix(oauth-docs): use verified SDK APIs and add PKCE support
steve-calvert-glean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -265,6 +265,110 @@ response, err := client.Client.Chat.Create(ctx, &glean.ChatRequest{ | |
| }) | ||
| ``` | ||
|
|
||
| ### OAuth Authentication | ||
|
|
||
| OAuth allows you to use access tokens from your identity provider (Google, Azure, Okta, etc.) instead of Glean-issued tokens. | ||
|
|
||
| :::info Prerequisites | ||
| - OAuth enabled in [Glean Admin > Third-Party OAuth](https://app.glean.com/admin/setup/third-party-oauth) | ||
| - Your OAuth Client ID registered with Glean | ||
| - See [OAuth Setup Guide](https://docs.glean.com/administration/oauth/oauth-idp) for admin configuration | ||
| ::: | ||
|
|
||
| OAuth requests require these headers: | ||
|
|
||
| | Header | Value | | ||
| |--------|-------| | ||
| | `Authorization` | `Bearer <oauth_access_token>` | | ||
| | `X-Glean-Auth-Type` | `OAUTH` | | ||
|
|
||
| #### Example: Authorization Code Flow | ||
|
|
||
| This example uses [golang.org/x/oauth2](https://pkg.go.dev/golang.org/x/oauth2): | ||
|
|
||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
| "os" | ||
|
|
||
| "golang.org/x/oauth2" | ||
| glean "github.com/gleanwork/api-client-go" | ||
| ) | ||
|
|
||
| var oauthConfig = &oauth2.Config{ | ||
| ClientID: os.Getenv("OAUTH_CLIENT_ID"), | ||
| ClientSecret: os.Getenv("OAUTH_CLIENT_SECRET"), | ||
| RedirectURL: "http://localhost:8080/callback", | ||
| Scopes: []string{"openid", "email"}, | ||
| Endpoint: oauth2.Endpoint{ | ||
| AuthURL: os.Getenv("OAUTH_AUTH_URL"), | ||
| TokenURL: os.Getenv("OAUTH_TOKEN_URL"), | ||
| }, | ||
| } | ||
|
|
||
| // oauthTransport adds OAuth headers to all requests | ||
| type oauthTransport struct { | ||
| token string | ||
| transport http.RoundTripper | ||
| } | ||
|
|
||
| func (t *oauthTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| req.Header.Set("Authorization", "Bearer "+t.token) | ||
| req.Header.Set("X-Glean-Auth-Type", "OAUTH") | ||
| return t.transport.RoundTrip(req) | ||
| } | ||
|
|
||
| func main() { | ||
| http.HandleFunc("/login", handleLogin) | ||
| http.HandleFunc("/callback", handleCallback) | ||
| http.ListenAndServe(":8080", nil) | ||
| } | ||
|
|
||
| func handleLogin(w http.ResponseWriter, r *http.Request) { | ||
| url := oauthConfig.AuthCodeURL("state") | ||
| http.Redirect(w, r, url, http.StatusTemporaryRedirect) | ||
| } | ||
|
Comment on lines
349
to
367
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4/5 (serious issue: blocking) Have we validated this code works? We're OAuth 2.1 so That we're generating a state suggests we might be not generating a PKCE challenge. |
||
|
|
||
| func handleCallback(w http.ResponseWriter, r *http.Request) { | ||
| code := r.URL.Query().Get("code") | ||
| token, err := oauthConfig.Exchange(context.Background(), code) | ||
| if err != nil { | ||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| // Create HTTP client with OAuth headers | ||
| httpClient := &http.Client{ | ||
| Transport: &oauthTransport{ | ||
| token: token.AccessToken, | ||
| transport: http.DefaultTransport, | ||
| }, | ||
| } | ||
|
|
||
| // Create Glean client with custom HTTP client | ||
| client := glean.New( | ||
| glean.WithInstance(os.Getenv("GLEAN_INSTANCE")), | ||
| glean.WithClient(httpClient), | ||
| ) | ||
|
|
||
| results, err := client.Client.Search.Query(r.Context(), "quarterly reports", nil) | ||
| if err != nil { | ||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| json.NewEncoder(w).Encode(results) | ||
| } | ||
| ``` | ||
|
|
||
| :::tip | ||
| Access tokens typically expire after ~1 hour. For production use, use `oauth2.Config.TokenSource` for automatic refresh. | ||
| ::: | ||
|
|
||
| ## Error Handling | ||
|
|
||
| ```go | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3/5 (strong opinion: non-blocking)
This should include
offline_accessto showcase getting a refresh token