Skip to content

Commit

Permalink
Add option to use OAuth "state" param in internet accounts (#3296)
Browse files Browse the repository at this point in the history
* Add option to use OAuth "state" param

* Make state not a getter
  • Loading branch information
garrettjstevens committed Nov 1, 2022
1 parent d085813 commit 14aa9fa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions plugins/authentication/src/OAuthModel/configSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ const OAuthConfigSchema = ConfigurationSchema(
type: 'string',
defaultValue: '',
},
/**
* #slot
*/
state: {
description: 'optional state for the authorization call',
type: 'string',
defaultValue: '',
},
/**
* #slot
*/
Expand Down
14 changes: 13 additions & 1 deletion plugins/authentication/src/OAuthModel/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface OAuthData {
code_challenge?: string
code_challenge_method?: string
token_access_type?: string
state?: string
}

function fixup(buf: string) {
Expand Down Expand Up @@ -70,6 +71,13 @@ const stateModelFactory = (configSchema: OAuthInternetAccountConfigModel) => {
get scopes(): string {
return getConf(self, 'scopes')
},
/**
* OAuth state parameter: https://www.rfc-editor.org/rfc/rfc6749#section-4.1.1
* Can override or extend if dynamic state is needed.
*/
state(): string | undefined {
return getConf(self, 'state') || undefined
},
get responseType(): 'token' | 'code' {
return getConf(self, 'responseType')
},
Expand Down Expand Up @@ -155,7 +163,7 @@ const stateModelFactory = (configSchema: OAuthInternetAccountConfigModel) => {
if (obj.error === 'invalid_grant') {
this.removeRefreshToken()
}
text ??= obj?.error_description
text = obj?.error_description ?? text
} catch (e) {
/* just use original text as error */
}
Expand Down Expand Up @@ -256,6 +264,10 @@ const stateModelFactory = (configSchema: OAuthInternetAccountConfigModel) => {
response_type: self.responseType || 'code',
}

if (self.state()) {
data.state = self.state()
}

if (self.scopes) {
data.scope = self.scopes
}
Expand Down

0 comments on commit 14aa9fa

Please sign in to comment.