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

oauth2: add new RawBody field to oauth2.Token #432

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aranw
Copy link

@aranw aranw commented Aug 15, 2020

This adds the ability to retrieve the raw HTTP response body from the token authorization flows. This is handy for services such as Slack as they include additional data in the response

For example, taking the Slack v2 oauth.access request https://api.slack.com/methods/oauth.v2.access

Gives you the following response

{
    "ok": true,
    "access_token": "xoxb-17653672481-19874698323-pdFZKVeTuE8sk7oOcBrzbqgy",
    "token_type": "bot",
    "scope": "commands,incoming-webhook",
    "bot_user_id": "U0KRQLJ9H",
    "app_id": "A0KRD7HC3",
    "team": {
        "name": "Slack Softball Team",
        "id": "T9TK3CUKW"
    },
    "enterprise": {
        "name": "slack-sports",
        "id": "E12345678"
    },
    "authed_user": {
        "id": "U1234",
        "scope": "chat:write",
        "access_token": "xoxp-1234",
        "token_type": "user"
    }
}

To gain access to this information a user of the golang/oauth2 library has to use the oauth2.Token.Extra() func. This is okay for a few fields and fields that are in the root JSON object but once you start dealing with nested fields this can become cumbersome

if botuserid, ok := token.Extra("bot_user_id").(string); ok {
    // use botuserid
}

if team, ok := token.Extra("team").(map[string]interface{}); ok {
	if teamid, ok := team["id"].(string); ok {
		// use teamid
	}
	if teamname, ok := team["name"].(string); ok {
		// use teamname
	}
}

This PR adds the ability for accessing the response body from the Token request and allows a consumer to decode it into a struct to simplify the retrieval of extra fields

This adds the ability to retrieve the raw HTTP response body
from the token authorization flows. This is handy for services
such as Slack as they include additional data in the response
@gopherbot
Copy link
Contributor

This PR (HEAD: d9f8b7d) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/oauth2/+/248682 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@gopherbot
Copy link
Contributor

Message from Gobot Gobot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://golang.org/doc/contribute.html#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.


Please don’t reply on this GitHub thread. Visit golang.org/cl/248682.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go Bot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://golang.org/doc/contribute.html#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.


Please don’t reply on this GitHub thread. Visit golang.org/cl/248682.
After addressing review feedback, remember to publish your drafts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants