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: access token validation #372

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions recipe/thirdparty/providers/config_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func getProviderConfigForClient(config tpmodels.ProviderConfig, clientConfig tpm
OIDCDiscoveryEndpoint: config.OIDCDiscoveryEndpoint,
UserInfoMap: config.UserInfoMap,
ValidateIdTokenPayload: config.ValidateIdTokenPayload,
ValidateAccessToken: config.ValidateAccessToken,
RequireEmail: config.RequireEmail,
GenerateFakeEmail: config.GenerateFakeEmail,
}
Expand Down
31 changes: 31 additions & 0 deletions recipe/thirdparty/providers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package providers

import (
"encoding/base64"
"errors"
"fmt"

Expand All @@ -36,6 +37,36 @@ func Github(input tpmodels.ProviderInput) *tpmodels.TypeProvider {
input.Config.TokenEndpoint = "https://github.com/login/oauth/access_token"
}

if input.Config.ValidateAccessToken == nil {
input.Config.ValidateAccessToken = func(accessToken string, clientConfig tpmodels.ProviderConfigForClientType, userContext supertokens.UserContext) error {
credentials := fmt.Sprintf("%s:%s", clientConfig.ClientID, clientConfig.ClientSecret)
basicAuthToken := base64.StdEncoding.EncodeToString([]byte(credentials))

resp, err := doPostRequest(
fmt.Sprintf("https://api.github.com/applications/%s/token", clientConfig.ClientID),
map[string]interface{}{
"access_token": accessToken,
},
map[string]interface{}{
"Authorization": fmt.Sprintf("Basic %s", basicAuthToken),
},
)
if err != nil {
return err
}

if _, ok := resp["app"]; !ok {
return errors.New("Access token does not belong to your application")
}

if resp["client_id"].(string) != clientConfig.ClientID {
return errors.New("Access token does not belong to your application")
}

return nil
}
}

oOverride := input.Override

input.Override = func(originalImplementation *tpmodels.TypeProvider) *tpmodels.TypeProvider {
Expand Down
7 changes: 7 additions & 0 deletions recipe/thirdparty/providers/oauth2_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ func oauth2_GetUserInfo(config tpmodels.ProviderConfigForClientType, oAuthTokens
}
}

if accessTokenOk && config.ValidateAccessToken != nil {
err := config.ValidateAccessToken(accessToken, config, userContext)
if err != nil {
return tpmodels.TypeUserInfo{}, err
}
}

if accessTokenOk && config.UserInfoEndpoint != "" {
headers := map[string]string{
"Authorization": "Bearer " + accessToken,
Expand Down
11 changes: 10 additions & 1 deletion recipe/thirdparty/tpmodels/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,15 @@ type ProviderConfig struct {
RequireEmail *bool `json:"requireEmail,omitempty"`

ValidateIdTokenPayload func(idTokenPayload map[string]interface{}, clientConfig ProviderConfigForClientType, userContext supertokens.UserContext) error `json:"-"`
GenerateFakeEmail func(thirdPartyUserId string, tenantId string, userContext supertokens.UserContext) string `json:"-"`

/**
* This function is responsible for validating the access token received from the third party provider.
* This check can include checking the expiry of the access token, checking the audience of the access token, etc.
*
* This function should return an error if the access token should be considered invalid, or return nothing if it is valid
*/
ValidateAccessToken func(accessToken string, clientConfig ProviderConfigForClientType, userContext supertokens.UserContext) error `json:"-"`
GenerateFakeEmail func(thirdPartyUserId string, tenantId string, userContext supertokens.UserContext) string `json:"-"`
}

type ProviderClientConfig struct {
Expand Down Expand Up @@ -158,6 +166,7 @@ type ProviderConfigForClientType struct {
OIDCDiscoveryEndpoint string
UserInfoMap TypeUserInfoMap
ValidateIdTokenPayload func(idTokenPayload map[string]interface{}, clientConfig ProviderConfigForClientType, userContext supertokens.UserContext) error
ValidateAccessToken func(accessToken string, clientConfig ProviderConfigForClientType, userContext supertokens.UserContext) error `json:"-"`

RequireEmail *bool
GenerateFakeEmail func(thirdPartyUserId string, tenantId string, userContext supertokens.UserContext) string
Expand Down
Loading