mulesoft-labs / raml-client-generator

Template-driven generator of clients for APIs described by a RAML spec
Other
121 stars 27 forks source link

Support Users in JavaScript #1

Closed blakeembrey closed 9 years ago

blakeembrey commented 10 years ago

How do we properly support the number of authentication and user methods. I would suppose that API instances can belong to a single user:

var github = new GithubApi({ user: { ... });
// Or...
github.options.user = { ... };

Also, every API request could override this:

github.resource.events.get(null, { user: { ... } });

Now, how does that intermediate format look? Do we use plain objects, auth instances or something else? Something like basic auth can be used directly in the user stage, but other methods can't be used without the authentication step to retrieve a token.

Object example:

github.options.user = { type: 'OAuth 2.0', token: 'abc', refreshToken: 'def' };
github.options.user = { type: 'Basic Authentication', username: 'abc', password: 'def' };

The downside of this approach is that if the data is being stored in a database they may not want to store an identical type with every entry. Also, strings are pretty meaningless and easy to get wrong. So the alternative may be the instance example:

github.options.user = new GithubApi.OAuth2('abc', 'def');
github.options.user = new GithubApi.BasicAuth('abc', 'def');

Somewhat more meaningful. I'll do a bit of tweaking this I imagine to get the most semantic flow. Maybe I'll look at what other libraries follow too and see if there are any other approaches. Personally, the instance approach seems the least error prone and easiest to encapsulate.

blakeembrey commented 9 years ago

OAuth 2.0 implementation is supported in the latest client release.