Skip to content

Commit

Permalink
service and app auth providers introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
dimsmol committed Jun 10, 2013
1 parent 56a33ce commit 765b8ba
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 58 deletions.
18 changes: 16 additions & 2 deletions lib/auth/auth_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
var authen = require('authen');
var Auth = require('../handlers/auth');
var CtxAdapter = require('./ctx_adapter');
var Provider = require('./provider');
var ServiceAuthProvider = require('./providers/service');
var AppAuthProvider = require('./providers/app');


var AuthUnit = function () {
this.units = null;
this.settings = null;
this.providerClasses = this.getProviderClasses();

this.adapter = null;
this.providers = {};
Expand All @@ -26,6 +28,14 @@ AuthUnit.prototype.unitInit = function (units) {
this.init();
};

AuthUnit.prototype.getProviderClasses = function () {
return {
user: authen.AuthProvider,
service: ServiceAuthProvider,
app: AppAuthProvider
};
};

AuthUnit.prototype.setRevokerFactory = function (f) {
this.revokerFactory = f;
};
Expand Down Expand Up @@ -95,7 +105,11 @@ AuthUnit.prototype.addProvider = function (provider) {
};

AuthUnit.prototype.createProvider = function (type, settings) {
return new Provider(settings);
var ProviderClass = this.providerClasses[type];
if (ProviderClass == null) {
ProviderClass = authen.AuthProvider;
}
return new ProviderClass(settings);
};

AuthUnit.prototype.initTokener = function (type, settings) {
Expand Down
56 changes: 0 additions & 56 deletions lib/auth/provider.js

This file was deleted.

39 changes: 39 additions & 0 deletions lib/auth/providers/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";
var inherits = require('util').inherits;
var AuthProvider = require('authen/lib/auth_provider');


var AppAuthProvider = function (opt_options) {
AuthProvider.call(this, opt_options);
};
inherits(AppAuthProvider, AuthProvider);


AppAuthProvider.prototype.packIdentity = function (identity) {
return JSON.stringify([identity.service, identity.user]);
};

AppAuthProvider.prototype.unpackIdentity = function (identityStr) {
var parsed = JSON.parse(identityStr);
return {
service: parsed[0],
user: parsed[1]
};
};

AppAuthProvider.prototype.prepareAuthResult = function (ctx, options, authResult, cb) {
var identity = this.getIdentity(authResult);
var result = {
type: identity.authType || this.defaultAuthType,
identity: identity.user,
via: {
type: this.type,
identity: identity.service,
authResult: authResult
}
};
cb(null, result);
};


module.exports = AppAuthProvider;
9 changes: 9 additions & 0 deletions lib/auth/providers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";
var ServiceAuthProvider = require('./service');
var AppAuthProvider = require('./app');


module.exports = {
Service: ServiceAuthProvider,
App: AppAuthProvider
};
47 changes: 47 additions & 0 deletions lib/auth/providers/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";
var inherits = require('util').inherits;
var AuthProvider = require('authen/lib/auth_provider');


var ServiceAuthProvider = function (opt_options) {
AuthProvider.call(this, opt_options);
};
inherits(ServiceAuthProvider, AuthProvider);


ServiceAuthProvider.prototype.prepareAuthResult = function (ctx, options, authResult, cb) {
var result = {
type: this.type,
identity: this.getIdentity(authResult),
authResult: result
};
if (options.allowOnBehalf && this.options.allowOnBehalf) {
var onBehalf = this.createOnBehalfResult(ctx);
if (onBehalf != null) {
var via = result;
result = onBehalf;
result.via = via;
}
}
cb(null, result);
};

ServiceAuthProvider.prototype.unpackOnBehalfIdentity = function (identityStr) {
return identityStr;
};

ServiceAuthProvider.prototype.createOnBehalfResult = function (ctx) {
var result = null;
var onBehalfData = this.adapter.extractOnBehalfData(ctx);
if (onBehalfData != null) {
result = {
type: onBehalfData.type || this.options.defaultOnBehalfAuthType,
identity: this.unpackOnBehalfIdentity(onBehalfData.identityStr),
onBehalfData: onBehalfData
};
}
return result;
};


module.exports = ServiceAuthProvider;

0 comments on commit 765b8ba

Please sign in to comment.