Skip to content

Commit

Permalink
Update persister_oauth2.go to handle special character | coming in th…
Browse files Browse the repository at this point in the history
…e scopes as part of consent request

Url encoded and decoded while fetching values from the table,  as "|" is a seperator used to store scopes
  • Loading branch information
Ajayn84 authored Sep 25, 2024
1 parent f83193f commit 1606831
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions persistence/sql/persister_oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func (p *Persister) sqlSchemaFromRequest(ctx context.Context, signature string,
RequestedAt: r.GetRequestedAt(),
InternalExpiresAt: sqlxx.NullTime(expiresAt),
Client: r.GetClient().GetID(),
Scopes: strings.Join(r.GetRequestedScopes(), "|"),
GrantedScope: strings.Join(r.GetGrantedScopes(), "|"),
Scopes: strings.Join(escapeDelimiter(r.GetRequestedScopes()), "|"),
GrantedScope: strings.Join(escapeDelimiter(r.GetGrantedScopes()), "|"),
GrantedAudience: strings.Join(r.GetGrantedAudience(), "|"),
RequestedAudience: strings.Join(r.GetRequestedAudience(), "|"),
Form: r.GetRequestForm().Encode(),
Expand Down Expand Up @@ -158,8 +158,8 @@ func (r *OAuth2RequestSQL) toRequest(ctx context.Context, session fosite.Session
RequestedAt: r.RequestedAt,
// ExpiresAt does not need to be populated as we get the expiry time from the session.
Client: c,
RequestedScope: stringsx.Splitx(r.Scopes, "|"),
GrantedScope: stringsx.Splitx(r.GrantedScope, "|"),
RequestedScope: unescapeDelimiter(r.Scopes),
GrantedScope: unescapeDelimiter(r.GrantedScope),
RequestedAudience: stringsx.Splitx(r.RequestedAudience, "|"),
GrantedAudience: stringsx.Splitx(r.GrantedAudience, "|"),
Form: val,
Expand Down Expand Up @@ -549,3 +549,30 @@ func (p *Persister) DeleteAccessTokens(ctx context.Context, clientID string) (er
p.QueryWithNetwork(ctx).Where("client_id=?", clientID).Delete(&OAuth2RequestSQL{Table: sqlTableAccess}),
)
}

func escapeDelimiter(scopes []string) []string {
escapedScopes := make([]string, len(scopes))
for i, scope := range scopes {
if strings.Contains(scope, "|") {
escapedScopes[i] = url.QueryEscape(scope)
} else {
escapedScopes[i] = scope
}
}
return escapedScopes
}

func unescapeDelimiter(scopes string) []string {
updatedScopes := stringsx.Splitx(scopes, "|")
if strings.Contains(scopes, "%26") {
for i, scope := range updatedScopes {
unescapedScope, err := url.QueryUnescape(scope)
if err != nil {
errors.Errorf("Error while url unescaping scope: %s", scope)

Check failure on line 571 in persistence/sql/persister_oauth2.go

View workflow job for this annotation

GitHub Actions / Run tests and lints

Error return value of `errors.Errorf` is not checked (errcheck)
}
updatedScopes[i] = unescapedScope
}
}
return updatedScopes
}

Check failure on line 578 in persistence/sql/persister_oauth2.go

View workflow job for this annotation

GitHub Actions / Run tests and lints

File is not `goimports`-ed (goimports)

0 comments on commit 1606831

Please sign in to comment.