-
Notifications
You must be signed in to change notification settings - Fork 621
feat: add user_token_getter_url, people can get user token from app manager #695
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
wangjingling
wants to merge
1
commit into
larksuite:main
Choose a base branch
from
wangjingling:feature/user_access_token_getter
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.
+1,212
−133
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package auth | ||
|
|
||
| // this file is a demo for user_token_getter_url | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
|
|
||
| "github.com/cloudwego/hertz/pkg/app" | ||
| "github.com/cloudwego/hertz/pkg/common/utils" | ||
| lark "github.com/larksuite/oapi-sdk-go/v3" | ||
| larkauthen "github.com/larksuite/oapi-sdk-go/v3/service/authen/v1" | ||
| ) | ||
|
|
||
| const ( | ||
| appID = "" | ||
| appSecret = "" | ||
| redirectURI | ||
| authHost = "open.feishu.cn/open.larksuite.com" | ||
| ) | ||
|
|
||
| var ( | ||
| larkClient = lark.NewClient(appID, appSecret) | ||
| ) | ||
|
|
||
| // UserAuth handles the initial step of the OAuth flow by redirecting the user to the Lark authorization page. | ||
| func UserAuth(ctx context.Context, c *app.RequestContext) { | ||
| state := c.Query("state") | ||
| if state == "" { | ||
| c.JSON(http.StatusBadRequest, utils.H{"error": "missing state"}) | ||
| return | ||
| } | ||
|
|
||
| scope := c.Query("scope") | ||
|
|
||
| authURLObj, _ := url.Parse(fmt.Sprintf("https://%s/open-apis/authen/v1/index", authHost)) | ||
| query := authURLObj.Query() | ||
| query.Set("redirect_uri", redirectURI) | ||
| query.Set("app_id", appID) | ||
| query.Set("state", state) | ||
| if scope != "" { | ||
| query.Set("scope", scope) | ||
| } | ||
| authURLObj.RawQuery = query.Encode() | ||
|
|
||
| c.Redirect(http.StatusFound, []byte(authURLObj.String())) | ||
| } | ||
|
|
||
| // OAuthCallback processes the OAuth callback from Lark, fetches the user access token, and sends it back to the local server. | ||
| func OAuthCallback(ctx context.Context, c *app.RequestContext) { | ||
| code := c.Query("code") | ||
| state := c.Query("state") | ||
|
|
||
| if code == "" { | ||
| c.JSON(http.StatusBadRequest, utils.H{"error": "missing code"}) | ||
| return | ||
| } | ||
|
|
||
| stateInt, err := strconv.Atoi(state) | ||
| if err != nil { | ||
| c.JSON(http.StatusBadRequest, utils.H{"error": "invalid state parameter, must be integer"}) | ||
| return | ||
| } | ||
|
|
||
| // get user_access_token | ||
| req := larkauthen.NewCreateAccessTokenReqBuilder(). | ||
| Body(larkauthen.NewCreateAccessTokenReqBodyBuilder(). | ||
| GrantType("authorization_code"). | ||
| Code(code). | ||
| Build()). | ||
| Build() | ||
|
|
||
| resp, err := larkClient.Authen.AccessToken.Create(ctx, req) | ||
| if err != nil { | ||
| c.JSON(http.StatusInternalServerError, utils.H{"error": "failed to get access token", "detail": err.Error()}) | ||
| return | ||
| } | ||
|
|
||
| if !resp.Success() { | ||
| c.JSON(http.StatusInternalServerError, utils.H{"error": "feishu API returned error", "code": resp.Code, "msg": resp.Msg}) | ||
| return | ||
| } | ||
|
|
||
| data := resp.Data | ||
| if data == nil || data.AccessToken == nil { | ||
| c.JSON(http.StatusInternalServerError, utils.H{"error": "empty data in response"}) | ||
| return | ||
| } | ||
|
|
||
| // TODO check user permission or scope | ||
|
|
||
| dataBytes, err := json.Marshal(data) | ||
| if err != nil { | ||
| c.JSON(http.StatusInternalServerError, utils.H{"error": "failed to marshal data"}) | ||
| return | ||
| } | ||
|
|
||
| html := fmt.Sprintf(`<!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Lark Token</title> | ||
| </head> | ||
| <body> | ||
| <p>Sending token...</p> | ||
| <script> | ||
| window.onload = function() { | ||
| var url = 'http://127.0.0.1:%d/user_access_token'; | ||
| var data = %s; | ||
| fetch(url, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json' | ||
| }, | ||
| body: JSON.stringify(data) | ||
| }).then(function(response) { | ||
| document.body.innerHTML += '<p>Token sended,close in 3 seconds...</p>'; | ||
| setTimeout(function() { | ||
| window.close(); | ||
| }, 3000); | ||
| }).catch(function(err) { | ||
| document.body.innerHTML += '<p>Token send fail: ' + err + '</p>'; | ||
| }); | ||
| }; | ||
| </script> | ||
| </body> | ||
| </html>`, stateInt, string(dataBytes)) | ||
|
|
||
| c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(html)) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Fix the demo authorization host.
authHostcurrently concatenates the Feishu and Larksuite domains, soUserAuthbuildshttps://open.feishu.cn/open.larksuite.com/open-apis/authen/v1/indexinstead of a real OAuth endpoint. Anyone copying this demo will hit a broken redirect immediately.Suggested fix
🤖 Prompt for AI Agents