Skip to content

Commit 461360b

Browse files
author
Matt Gaspar
committed
Added support for token-based authentication
1 parent 80931fe commit 461360b

File tree

2 files changed

+70
-31
lines changed

2 files changed

+70
-31
lines changed

README.md

+35-18
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ _PS: This is actually also a gulp plugin._
77
## Required [![Dependency Status][david-image]][david-url] [![devDependency Status][david-image-dev]][david-url-dev]
88

99
* node.js 4+
10-
11-
10+
11+
1212

1313
## Quick start
1414
```bash
@@ -19,8 +19,8 @@ _PS: This is actually also a gulp plugin._
1919
- Create a `nsconfig.json` file in the root of you project with at least __email__ , __password__, __account__, __script__ number and __deployment__ number.
2020

2121
- Use it with gulp or with the CLI (see CLI section below)
22-
23-
22+
23+
2424
```javascript
2525
var nscabinet = require('nscabinet');
2626
gulp.src('myProject/dist/**/*.js').pipe(nscabinet({ rootPath : '/Templates' }));
@@ -68,7 +68,7 @@ __Connection__
6868
* `script`
6969

7070
* `deployment` defaults to 1.
71-
71+
7272
* `conffile` overrides default `nsconfig.json` file name, allowing multiple project setting.
7373

7474
__Path__
@@ -78,6 +78,23 @@ __Path__
7878
Example: Upload file with path `img/image.jpg` to rootPath `/Templates` will "upsert" the file
7979
onto '/Templates/img/image.jpg'.
8080

81+
__Token Based Authentication__
82+
83+
* `consumerKey`
84+
85+
* `consumerSecret`
86+
87+
* `token`
88+
89+
* `tokenSecret`
90+
91+
Setup:
92+
- Enable Token-based Authentication (Enable Features > SuiteCloud > Manage Authentication)
93+
- Create an integration record to generate a consumer key and secret
94+
- Generate a user token (must enable a role with User Access Tokens permission - Administrator role cannot be used)
95+
96+
The email and password are ignored when token based authentication is used. (They are still required but can just have placeholder values)
97+
8198
## nscabinet.upload
8299

83100
```javascript
@@ -113,25 +130,25 @@ nscabinet.download(['MyProject/*.js','/Web Site Hosting Files/My Site/*.html'])
113130
```
114131

115132
* `files` file selector (one or many).
116-
133+
117134
* `*` is accepted on the file part. The restlet then runs a file search by name
118135
in which `*` is replaced with `%`.
119-
136+
120137
* Paths are also relative to `opts.rootPath`. If a file selector begins with `/`, files will be queried
121138
by absolute path in netsuite, but saved locally inside the `cabinet_root` folder.
122-
139+
123140
* If a path has `/**/`, a recursive search will be done. This can be used to search
124141
just by file name, regardless of path.
125-
142+
126143
* (PS: While the syntax is similar, don't expect full glob funcionality. It's not a glob!)
127-
128-
144+
145+
129146
* `opts` Common options.
130147

131148

132149
## nscabinet.url ( file : string , [opts] ) : Promise[string]
133150

134-
Get the url (internal or external) of a cabinet file. Returns a promise.
151+
Get the url (internal or external) of a cabinet file. Returns a promise.
135152
Useful for email campaign stuff.
136153

137154
Options: receives the ones which make sense here (ex: rootPath, realm, etc...) in the
@@ -176,18 +193,18 @@ Uploading Views/view.html to /SuiteScripts
176193
## Contributing
177194

178195
- If you add new funcionality, also add a new test!
179-
196+
180197
At the time tests are run locally. To set up the tests:
181198

182199
- Install the restlet in an available account;
183-
200+
184201
- Set up nsconfig.json, pointing to that account; Don't forget to set
185202
a rootPath to where the tests will play around and create its lots of files;
186-
203+
187204
- Run 'gulp'
188-
189-
190-
205+
206+
207+
191208
[npm-url]: https://npmjs.org/package/nscabinet
192209
[npm-image]: http://img.shields.io/npm/v/nscabinet.svg
193210

src/nscabinet.js

+35-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
var request = require('request'),
66
through = require('through2'),
77
vinyl = require('vinyl'),
8-
nsconfig = require('nsconfig'),
8+
nsconfig = require('../../nsconfig'),
99
path = require('path');
1010

1111
var PARAMS_DEF = [
@@ -69,7 +69,7 @@ function download (files,params,info) {
6969
files : files ,
7070
rootpath: params.rootPath
7171
};
72-
var buffer = '';
72+
var buffer = '';
7373
var emitter = through.obj(
7474
function transform(data,enc,cb) {
7575
buffer += data;
@@ -158,17 +158,39 @@ function _requestOpts (params) {
158158
server = process.env.NS_SERVER || `https://rest.${params.realm}/app/site/hosting/restlet.nl`;
159159
//NS_SERVER = testing + nsmockup
160160

161-
return {
162-
url: server,
163-
qs: {
164-
script: params.script,
165-
deploy: params.deployment
166-
},
167-
method : 'POST' ,
168-
headers: {
169-
authorization: `NLAuth nlauth_account=${params.account},nlauth_email=${params.email},nlauth_signature=${params.password}${nlauthRolePortion}`
170-
}
171-
};
161+
if (params.token) {
162+
163+
return {
164+
url: server,
165+
qs: {
166+
script: params.script,
167+
deploy: params.deployment
168+
},
169+
method: 'POST',
170+
oauth: {
171+
consumer_key: params.consumerKey,
172+
consumer_secret: params.consumerSecret,
173+
token: params.token,
174+
token_secret: params.tokenSecret,
175+
realm: params.account
176+
}
177+
};
178+
179+
} else {
180+
181+
return {
182+
url: server,
183+
qs: {
184+
script: params.script,
185+
deploy: params.deployment
186+
},
187+
method : 'POST' ,
188+
headers: {
189+
authorization: `NLAuth nlauth_account=${params.account},nlauth_email=${params.email},nlauth_signature=${params.password}${nlauthRolePortion}`
190+
}
191+
};
192+
193+
}
172194
}
173195

174196

0 commit comments

Comments
 (0)