Skip to content

Commit 23c2d7c

Browse files
authored
Merge pull request #70 from djsteinmetz/customroles
Add optional customRoles array to auth methods
2 parents 1235e9d + 42ad9d5 commit 23c2d7c

6 files changed

Lines changed: 188 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ All notable changes to the ordercloud-javascript-sdk will be documented in this
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [4.7.8] - 2022-05-04
9+
### Added
10+
- Added an optional `customRoles` array to the authentication methods to support authenticating with custom roles, in addition to standard ApiRoles.
811
## [4.6.8] - 2022-05-02
912
### Fixed
1013
- Docs now up to date with API v1.0.235 and previous changes to DecodedToken.role
14+
### Removed
15+
- The ApiRole `InventoryAdmin` was removed from the OrderCloud API and is now considered a custom role. This role was removed from the `ApiRole` type definition. This could be a breaking change if your application uses the role `InventoryAdmin`. In this case, we recommend making this role a custom role.
1116
## [4.6.7] - 2022-04-27
1217
### Fixed
1318
- DecodedToken.role was not properly typed. It is now (ApiRole[] | ApiRole | undefined)

codegen/templates/api/Auth.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Auth {
3232
* @param password of the user logging in
3333
* @param client_id of the application the user is logging into
3434
* @param scope roles being requested - space delimited string or array
35+
* @param customRoles optional custom roles being requested - string array
3536
* @param requestOptions.cancelToken Provide an [axios cancelToken](https://github.com/axios/axios#cancellation) that can be used to cancel the request.
3637
* @param requestOptions.requestType Provide a value that can be used to identify the type of request. Useful for error logs.
3738
*/
@@ -40,6 +41,7 @@ class Auth {
4041
password: string,
4142
clientID: string,
4243
scope: ApiRole[],
44+
customRoles?: string[],
4345
requestOptions: {
4446
cancelToken?: CancelToken
4547
requestType?: string
@@ -48,12 +50,18 @@ class Auth {
4850
if (!Array.isArray(scope)) {
4951
throw new Error('scope must be a string array')
5052
}
53+
if (customRoles != null && !Array.isArray(customRoles)) {
54+
throw new Error('custom roles must be defined as a string array')
55+
}
56+
var _scope = customRoles?.length
57+
? `${scope.join(' ')} ${customRoles.join(' ')}`
58+
: scope.join(' ')
5159
const body = {
5260
grant_type: 'password',
5361
username,
5462
password,
5563
client_id: clientID,
56-
scope: scope.join(' '),
64+
scope: _scope,
5765
}
5866
const configuration = Configuration.Get()
5967
const response = await axios
@@ -81,6 +89,7 @@ class Auth {
8189
* @param password of the user logging in
8290
* @param clientID of the application the user is logging into
8391
* @param scope roles being requested - space delimited string or array
92+
* @param customRoles optional custom roles being requested - string array
8493
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
8594
* @param reportProgress flag to report request and response progress.
8695
* @param requestOptions.cancelToken Provide an [axios cancelToken](https://github.com/axios/axios#cancellation) that can be used to cancel the request.
@@ -92,6 +101,7 @@ class Auth {
92101
password: string,
93102
clientID: string,
94103
scope: ApiRole[],
104+
customRoles?: string[],
95105
requestOptions: {
96106
cancelToken?: CancelToken
97107
requestType?: string
@@ -100,9 +110,15 @@ class Auth {
100110
if (!Array.isArray(scope)) {
101111
throw new Error('scope must be a string array')
102112
}
113+
if (customRoles != null && !Array.isArray(customRoles)) {
114+
throw new Error('custom roles must be defined as a string array')
115+
}
116+
var _scope = customRoles?.length
117+
? `${scope.join(' ')} ${customRoles.join(' ')}`
118+
: scope.join(' ')
103119
const body = {
104120
grant_type: 'password',
105-
scope: scope.join(' '),
121+
scope: _scope,
106122
client_id: clientID,
107123
username,
108124
password,
@@ -132,6 +148,7 @@ class Auth {
132148
* @param clientSecret of the application
133149
* @param clientID of the application the user is logging into
134150
* @param scope roles being requested - space delimited string or array
151+
* @param customRoles optional custom roles being requested - string array
135152
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
136153
* @param reportProgress flag to report request and response progress.
137154
* @param requestOptions.cancelToken Provide an [axios cancelToken](https://github.com/axios/axios#cancellation) that can be used to cancel the request.
@@ -141,6 +158,7 @@ class Auth {
141158
clientSecret: string,
142159
clientID: string,
143160
scope: ApiRole[],
161+
customRoles?: string[],
144162
requestOptions: {
145163
cancelToken?: CancelToken
146164
requestType?: string
@@ -149,9 +167,15 @@ class Auth {
149167
if (!Array.isArray(scope)) {
150168
throw new Error('scope must be a string array')
151169
}
170+
if (customRoles != null && !Array.isArray(customRoles)) {
171+
throw new Error('custom roles must be defined as a string array')
172+
}
173+
var _scope = customRoles?.length
174+
? `${scope.join(' ')} ${customRoles.join(' ')}`
175+
: scope.join(' ')
152176
const body = {
153177
grant_type: 'client_credentials',
154-
scope: scope.join(' '),
178+
scope: _scope,
155179
client_id: clientID,
156180
client_secret: clientSecret,
157181
}
@@ -217,12 +241,14 @@ class Auth {
217241
*
218242
* @param clientID of the application the user is logging into
219243
* @param scope roles being requested - space delimited string or array
244+
* @param customRoles optional custom roles being requested - string array
220245
* @param requestOptions.cancelToken Provide an [axios cancelToken](https://github.com/axios/axios#cancellation) that can be used to cancel the request.
221246
* @param requestOptions.requestType Provide a value that can be used to identify the type of request. Useful for error logs.
222247
*/
223248
public async Anonymous(
224249
clientID: string,
225250
scope: ApiRole[],
251+
customRoles?: string[],
226252
requestOptions: {
227253
cancelToken?: CancelToken
228254
requestType?: string
@@ -231,10 +257,16 @@ class Auth {
231257
if (!Array.isArray(scope)) {
232258
throw new Error('scope must be a string array')
233259
}
260+
if (customRoles != null && !Array.isArray(customRoles)) {
261+
throw new Error('custom roles must be defined as a string array')
262+
}
263+
var _scope = customRoles?.length
264+
? `${scope.join(' ')} ${customRoles.join(' ')}`
265+
: scope.join(' ')
234266
const body = {
235267
grant_type: 'client_credentials',
236268
client_id: clientID,
237-
scope: scope.join(' '),
269+
scope: _scope,
238270
}
239271
const configuration = Configuration.Get()
240272
const response = await axios

0 commit comments

Comments
 (0)