feat(#1003): oauth2 support - resourceOwnerPasswordCredentials, authorization code, client credentials (#1654)

* feat(#1003): oauth2 support
Co-authored-by: lohit-1 <lohit@usebruno.com>
This commit is contained in:
lohit
2024-02-26 16:44:38 +05:30
committed by GitHub
parent a4b13d5c2a
commit 9f81e6dc73
53 changed files with 1622 additions and 77 deletions

View File

@ -23,7 +23,7 @@ const { outdentString } = require('../../v1/src/utils');
*/
const grammar = ohm.grammar(`Bru {
BruFile = (meta | http | query | headers | auths | bodies | varsandassert | script | tests | docs)*
auths = authawsv4 | authbasic | authbearer | authdigest
auths = authawsv4 | authbasic | authbearer | authdigest | authOAuth2
bodies = bodyjson | bodytext | bodyxml | bodysparql | bodygraphql | bodygraphqlvars | bodyforms | body
bodyforms = bodyformurlencoded | bodymultipart
@ -80,6 +80,7 @@ const grammar = ohm.grammar(`Bru {
authbasic = "auth:basic" dictionary
authbearer = "auth:bearer" dictionary
authdigest = "auth:digest" dictionary
authOAuth2 = "auth:oauth2" dictionary
body = "body" st* "{" nl* textblock tagend
bodyjson = "body:json" st* "{" nl* textblock tagend
@ -380,6 +381,46 @@ const sem = grammar.createSemantics().addAttribute('ast', {
}
};
},
authOAuth2(_1, dictionary) {
const auth = mapPairListToKeyValPairs(dictionary.ast, false);
const grantTypeKey = _.find(auth, { name: 'grant_type' });
const usernameKey = _.find(auth, { name: 'username' });
const passwordKey = _.find(auth, { name: 'password' });
const callbackUrlKey = _.find(auth, { name: 'callback_url' });
const authorizationUrlKey = _.find(auth, { name: 'authorization_url' });
const accessTokenUrlKey = _.find(auth, { name: 'access_token_url' });
const clientIdKey = _.find(auth, { name: 'client_id' });
const clientSecretKey = _.find(auth, { name: 'client_secret' });
const scopeKey = _.find(auth, { name: 'scope' });
return {
auth: {
oauth2:
grantTypeKey?.value && grantTypeKey?.value == 'password'
? {
grantType: grantTypeKey ? grantTypeKey.value : '',
username: usernameKey ? usernameKey.value : '',
password: passwordKey ? passwordKey.value : ''
}
: grantTypeKey?.value && grantTypeKey?.value == 'authorization_code'
? {
grantType: grantTypeKey ? grantTypeKey.value : '',
callbackUrl: callbackUrlKey ? callbackUrlKey.value : '',
authorizationUrl: authorizationUrlKey ? authorizationUrlKey.value : '',
accessTokenUrl: accessTokenUrlKey ? accessTokenUrlKey.value : '',
clientId: clientIdKey ? clientIdKey.value : '',
clientSecret: clientSecretKey ? clientSecretKey.value : '',
scope: scopeKey ? scopeKey.value : ''
}
: grantTypeKey?.value && grantTypeKey?.value == 'client_credentials'
? {
grantType: grantTypeKey ? grantTypeKey.value : '',
clientId: clientIdKey ? clientIdKey.value : '',
clientSecret: clientSecretKey ? clientSecretKey.value : ''
}
: {}
}
};
},
bodyformurlencoded(_1, dictionary) {
return {
body: {

View File

@ -126,6 +126,42 @@ ${indentString(`password: ${auth.digest.password}`)}
`;
}
if (auth && auth.oauth2) {
switch (auth?.oauth2?.grantType) {
case 'password':
bru += `auth:oauth2 {
${indentString(`grant_type: password`)}
${indentString(`username: ${auth.oauth2.username}`)}
${indentString(`password: ${auth.oauth2.password}`)}
}
`;
break;
case 'authorization_code':
bru += `auth:oauth2 {
${indentString(`grant_type: authorization_code`)}
${indentString(`callback_url: ${auth.oauth2.callbackUrl}`)}
${indentString(`authorization_url: ${auth.oauth2.authorizationUrl}`)}
${indentString(`access_token_url: ${auth.oauth2.accessTokenUrl}`)}
${indentString(`client_id: ${auth.oauth2.clientId}`)}
${indentString(`client_secret: ${auth.oauth2.clientSecret}`)}
${indentString(`scope: ${auth.oauth2.scope}`)}
}
`;
break;
case 'client_credentials':
bru += `auth:oauth2 {
${indentString(`grant_type: client_credentials`)}
${indentString(`client_id: ${auth.oauth2.clientId}`)}
${indentString(`client_secret: ${auth.oauth2.clientSecret}`)}
}
`;
break;
}
}
if (body && body.json && body.json.length) {
bru += `body:json {
${indentString(body.json)}

View File

@ -45,6 +45,15 @@ auth:digest {
password: secret
}
auth:oauth2 {
grantType: authorization_code
client_id: client_id_1
client_secret: client_secret_1
auth_url: http://localhost:8080/api/auth/oauth2/ac/authorize
callback_url: http://localhost:8080/api/auth/oauth2/ac/callback
access_token_url: http://localhost:8080/api/auth/oauth2/ac/token
}
body:json {
{
"hello": "world"

View File

@ -63,6 +63,14 @@
"digest": {
"username": "john",
"password": "secret"
},
"oauth2": {
"grantType": "authorization_code",
"client_id": "client_id_1",
"client_secret": "client_secret_1",
"auth_url": "http://localhost:8080/api/auth/oauth2/ac/authorize",
"callback_url": "http://localhost:8080/api/auth/oauth2/ac/callback",
"access_token_url": "http://localhost:8080/api/auth/oauth2/ac/token"
}
},
"body": {