Skip to content

Commit 8c01c42

Browse files
authored
Merge pull request #29 from codeccoop/feat/bearer-credentials
bearer credentials and pkce oauth flow
2 parents a337958 + 6859234 commit 8c01c42

File tree

14 files changed

+63
-59
lines changed

14 files changed

+63
-59
lines changed

forms-bridge/addons/gcalendar/hooks.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function ( $defaults, $addon, $schema ) {
5050
'ref' => '#credential',
5151
'name' => 'schema',
5252
'type' => 'text',
53-
'value' => 'Bearer',
53+
'value' => 'OAuth',
5454
),
5555
array(
5656
'ref' => '#credential',
@@ -159,7 +159,7 @@ function ( $defaults, $addon, $schema ) {
159159
),
160160
'credential' => array(
161161
'name' => '',
162-
'schema' => 'Bearer',
162+
'schema' => 'OAuth',
163163
'oauth_url' => 'https://accounts.google.com/o/oauth2/v2',
164164
'scope' => 'https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/calendar.events',
165165
'client_id' => '',

forms-bridge/addons/gsheets/hooks.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function ( $defaults, $addon, $schema ) {
5959
'ref' => '#credential',
6060
'name' => 'schema',
6161
'type' => 'text',
62-
'value' => 'Bearer',
62+
'value' => 'OAuth',
6363
),
6464
array(
6565
'ref' => '#credential',
@@ -144,7 +144,7 @@ function ( $defaults, $addon, $schema ) {
144144
),
145145
'credential' => array(
146146
'name' => '',
147-
'schema' => 'Bearer',
147+
'schema' => 'OAuth',
148148
'oauth_url' => 'https://accounts.google.com/o/oauth2/v2',
149149
'scope' => 'https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/spreadsheets',
150150
'client_id' => '',

forms-bridge/addons/slack/hooks.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function ( $defaults, $addon, $schema ) {
3030
'ref' => '#credential',
3131
'name' => 'schema',
3232
'type' => 'text',
33-
'value' => 'Bearer',
33+
'value' => 'OAuth',
3434
),
3535
array(
3636
'ref' => '#credential',
@@ -281,7 +281,7 @@ function ( $defaults, $addon, $schema ) {
281281
),
282282
'credential' => array(
283283
'name' => '',
284-
'schema' => 'Bearer',
284+
'schema' => 'OAuth',
285285
'oauth_url' => 'https://slack.com/oauth/v2',
286286
'scope' => 'chat:write,channels:read,users:read',
287287
'client_id' => '',

forms-bridge/addons/zoho/hooks.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function ( $defaults, $addon, $schema ) {
3030
'ref' => '#credential',
3131
'name' => 'schema',
3232
'type' => 'text',
33-
'value' => 'Bearer',
33+
'value' => 'OAuth',
3434
),
3535
array(
3636
'ref' => '#credential',
@@ -172,7 +172,7 @@ function ( $defaults, $addon, $schema ) {
172172
),
173173
'credential' => array(
174174
'name' => '',
175-
'schema' => 'Bearer',
175+
'schema' => 'OAuth',
176176
'oauth_url' => 'https://accounts.{region}/oauth/v2',
177177
'scope' => 'ZohoCRM.modules.ALL,ZohoCRM.settings.modules.READ,ZohoCRM.settings.layouts.READ,ZohoCRM.users.READ',
178178
'client_id' => '',
@@ -182,13 +182,8 @@ function ( $defaults, $addon, $schema ) {
182182
'refresh_token' => '',
183183
),
184184
'backend' => array(
185+
'name' => 'Zoho API',
185186
'base_url' => 'https://www.zohoapis.{region}',
186-
'headers' => array(
187-
array(
188-
'name' => 'Accept',
189-
'value' => 'application/json',
190-
),
191-
),
192187
),
193188
),
194189
$defaults,

forms-bridge/deps/http

Submodule http updated from 225bbf9 to 2c1815f

forms-bridge/migrations/4.3.1.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Database migration to version 4.0.7
4+
*
5+
* @package formsbridge
6+
*/
7+
8+
// phpcs:disable WordPress.Files.FileName
9+
10+
if ( ! defined( 'ABSPATH' ) ) {
11+
exit();
12+
}
13+
14+
/**
15+
* Migration 4.3.1
16+
*/
17+
function forms_bridge_migration_431() {
18+
$http = get_option( 'forms-bridge_http', array() ) ?: array(
19+
'backends' => array(),
20+
'credentials' => array(),
21+
);
22+
23+
foreach ( $http['credentials'] as &$credential ) {
24+
if ( 'Bearer' === $credential['schema'] ) {
25+
$credential['schema'] = 'OAuth';
26+
}
27+
}
28+
29+
update_option( 'forms-bridge_http', $http );
30+
}

src/components/Backend/Authentication.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const OPTIONS = [
88
{ label: "Basic", value: "Basic" },
99
{ label: "Token", value: "Token" },
1010
{ label: "Bearer", value: "Bearer" },
11+
{ label: "OAuth", value: "OAuth" },
1112
];
1213

1314
export default function BackendAuthentication({ data = {}, setData }) {
@@ -23,7 +24,7 @@ export default function BackendAuthentication({ data = {}, setData }) {
2324
__nextHasNoMarginBottom
2425
/>
2526
</FieldWrapper>
26-
{data.schema && data.schema !== "Bearer" && (
27+
{data.schema && data.schema !== "OAuth" && (
2728
<FieldWrapper>
2829
<TextControl
2930
label={__("Client ID", "forms-bridge")}

src/components/Credential/AuthorizeButton.jsx

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,20 @@ export default function AuthorizeButton({ addon, data }) {
4747
method: "POST",
4848
data: { credential: data },
4949
})
50-
.then(({ success, redirect_url }) => {
50+
.then(({ success, data }) => {
5151
if (!success) throw "error";
5252

53+
const { url, params } = data;
5354
const form = document.createElement("form");
54-
form.action = redirect_url;
55+
form.action = url;
5556
form.method = "GET";
5657
form.target = "_blank";
5758

58-
let innerHTML = `
59-
<input name="client_id" value="${data.client_id}" />
60-
<input name="response_type" value="code" />
61-
<input name="redirect_uri" value="${restUrl("http-bridge/v1/oauth/redirect")}" />
62-
<input name="access_type" value="offline" />
63-
<input name="state" value="${btoa(addon)}" />
64-
`;
65-
66-
if (data.scope) {
67-
innerHTML += `<input name="scope" value="${data.scope}" />`;
68-
}
69-
70-
form.innerHTML = innerHTML;
59+
form.innerHTML = Object.keys(params).reduce((html, name) => {
60+
const value = params[name];
61+
if (!value) return html;
62+
return html + `<input name="${name}" value="${value}" />`;
63+
}, "");
7164

7265
form.style.visibility = "hidden";
7366
document.body.appendChild(form);

src/components/Templates/Wizard/useAuthorizedCredential.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export default function useAuthorizedCredential({ data = {}, fields = [] }) {
6464
setError(false);
6565
}, [credential]);
6666

67-
const isOauth = data.schema === "Bearer";
67+
const isOauth = data.schema === "OAuth";
6868

6969
const authorized = useMemo(() => {
7070
if (!isOauth || !!data.refresh_token) return true;
@@ -107,27 +107,20 @@ export default function useAuthorizedCredential({ data = {}, fields = [] }) {
107107
method: "POST",
108108
data: { credential },
109109
})
110-
.then(({ success, redirect_url }) => {
110+
.then(({ success, data }) => {
111111
if (!success) throw "error";
112112

113+
const { url, params } = data;
113114
const form = document.createElement("form");
115+
form.action = url;
114116
form.method = "GET";
115-
form.action = redirect_url;
116117
form.target = "_blank";
117118

118-
let innerHTML = `
119-
<input name="client_id" value="${credential.client_id}" />
120-
<input name="response_type" value="code" />
121-
<input name="redirect_uri" value="${restUrl("http-bridge/v1/oauth/redirect")}" />
122-
<input name="access_type" value="offline" />
123-
<input name="state" value="${btoa(addon)}" />
124-
`;
125-
126-
if (credential.scope) {
127-
innerHTML += `<input name="scope" value="${credential.scope}" />`;
128-
}
129-
130-
form.innerHTML = innerHTML;
119+
form.innerHTML = Object.keys(params).reduce((html, name) => {
120+
const value = params[name];
121+
if (!value) return html;
122+
return html + `<input name="${name}" value="${value}" />`;
123+
}, "");
131124

132125
form.style.visibility = "hidden";
133126
document.body.appendChild(form);

src/lib/utils.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ export function validateBackend(data) {
3939
return false;
4040
}
4141

42-
if (data.authentication?.type) {
43-
isValid = isValid && data.authentication.client_secret;
44-
45-
if (data.authentication.type !== "Bearer") {
46-
isValid = isValid && data.authentication.client_id;
47-
}
48-
}
49-
5042
return isValid;
5143
}
5244

0 commit comments

Comments
 (0)