nitrogen 0.2.23 → 0.29.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -109
- package/lib/Logger.d.ts +10 -0
- package/lib/Logger.js +56 -0
- package/lib/autolinking/Autolinking.d.ts +8 -0
- package/lib/autolinking/Autolinking.js +1 -0
- package/lib/autolinking/android/createCMakeExtension.d.ts +6 -0
- package/lib/autolinking/android/createCMakeExtension.js +106 -0
- package/lib/autolinking/android/createGradleExtension.d.ts +5 -0
- package/lib/autolinking/android/createGradleExtension.js +36 -0
- package/lib/autolinking/android/createHybridObjectInitializer.d.ts +2 -0
- package/lib/autolinking/android/createHybridObjectInitializer.js +159 -0
- package/lib/autolinking/createAndroidAutolinking.d.ts +11 -0
- package/lib/autolinking/createAndroidAutolinking.js +13 -0
- package/lib/autolinking/createIOSAutolinking.d.ts +5 -0
- package/lib/autolinking/createIOSAutolinking.js +19 -0
- package/lib/autolinking/ios/createHybridObjectInitializer.d.ts +9 -0
- package/lib/autolinking/ios/createHybridObjectInitializer.js +97 -0
- package/lib/autolinking/ios/createPodspecRubyExtension.d.ts +5 -0
- package/lib/autolinking/ios/createPodspecRubyExtension.js +69 -0
- package/lib/autolinking/ios/createSwiftCxxBridge.d.ts +2 -0
- package/lib/autolinking/ios/createSwiftCxxBridge.js +117 -0
- package/lib/autolinking/ios/createSwiftUmbrellaHeader.d.ts +3 -0
- package/lib/autolinking/ios/createSwiftUmbrellaHeader.js +74 -0
- package/lib/config/NitroConfig.d.ts +52 -0
- package/lib/config/NitroConfig.js +112 -0
- package/lib/config/NitroUserConfig.d.ts +22 -0
- package/lib/config/NitroUserConfig.js +88 -0
- package/lib/config/getConfig.d.ts +2 -0
- package/lib/config/getConfig.js +84 -0
- package/lib/createGitAttributes.d.ts +1 -0
- package/lib/createGitAttributes.js +11 -0
- package/lib/createPlatformSpec.d.ts +4 -0
- package/lib/createPlatformSpec.js +127 -0
- package/lib/getFiles.d.ts +1 -0
- package/lib/getFiles.js +28 -0
- package/lib/getPlatformSpecs.d.ts +17 -0
- package/lib/getPlatformSpecs.js +153 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +113 -10
- package/lib/init.d.ts +1 -0
- package/lib/init.js +123 -0
- package/lib/nitrogen.d.ts +11 -0
- package/lib/nitrogen.js +165 -0
- package/lib/prettifyDirectory.d.ts +1 -0
- package/lib/prettifyDirectory.js +27 -0
- package/lib/syntax/BridgedType.d.ts +41 -0
- package/lib/syntax/BridgedType.js +1 -0
- package/lib/syntax/CodeNode.d.ts +23 -0
- package/lib/syntax/CodeNode.js +1 -0
- package/lib/syntax/HybridObjectSpec.d.ts +13 -0
- package/lib/syntax/HybridObjectSpec.js +1 -0
- package/lib/syntax/Method.d.ts +41 -0
- package/lib/syntax/Method.js +108 -0
- package/lib/syntax/Parameter.d.ts +15 -0
- package/lib/syntax/Parameter.js +65 -0
- package/lib/syntax/Property.d.ts +50 -0
- package/lib/syntax/Property.js +147 -0
- package/lib/syntax/SourceFile.d.ts +70 -0
- package/lib/syntax/SourceFile.js +7 -0
- package/lib/syntax/c++/CppEnum.d.ts +6 -0
- package/lib/syntax/c++/CppEnum.js +110 -0
- package/lib/syntax/c++/CppHybridObject.d.ts +3 -0
- package/lib/syntax/c++/CppHybridObject.js +146 -0
- package/lib/syntax/c++/CppHybridObjectRegistration.d.ts +17 -0
- package/lib/syntax/c++/CppHybridObjectRegistration.js +18 -0
- package/lib/syntax/c++/CppStruct.d.ts +3 -0
- package/lib/syntax/c++/CppStruct.js +108 -0
- package/lib/syntax/c++/CppUnion.d.ts +6 -0
- package/lib/syntax/c++/CppUnion.js +88 -0
- package/lib/syntax/c++/getForwardDeclaration.d.ts +3 -0
- package/lib/syntax/c++/getForwardDeclaration.js +14 -0
- package/lib/syntax/c++/includeNitroHeader.d.ts +7 -0
- package/lib/syntax/c++/includeNitroHeader.js +34 -0
- package/lib/syntax/createType.d.ts +15 -0
- package/lib/syntax/createType.js +303 -0
- package/lib/syntax/getAllTypes.d.ts +3 -0
- package/lib/syntax/getAllTypes.js +11 -0
- package/lib/syntax/getCustomTypeConfig.d.ts +8 -0
- package/lib/syntax/getCustomTypeConfig.js +53 -0
- package/lib/syntax/getHybridObjectName.d.ts +36 -0
- package/lib/syntax/getHybridObjectName.js +10 -0
- package/lib/syntax/getInterfaceProperties.d.ts +4 -0
- package/lib/syntax/getInterfaceProperties.js +9 -0
- package/lib/syntax/getReferencedTypes.d.ts +2 -0
- package/lib/syntax/getReferencedTypes.js +47 -0
- package/lib/syntax/helpers.d.ts +13 -0
- package/lib/syntax/helpers.js +53 -0
- package/lib/syntax/isCoreType.d.ts +9 -0
- package/lib/syntax/isCoreType.js +47 -0
- package/lib/syntax/kotlin/FbjniHybridObject.d.ts +3 -0
- package/lib/syntax/kotlin/FbjniHybridObject.js +261 -0
- package/lib/syntax/kotlin/JNINativeRegistrations.d.ts +8 -0
- package/lib/syntax/kotlin/JNINativeRegistrations.js +7 -0
- package/lib/syntax/kotlin/KotlinBoxedPrimitive.d.ts +7 -0
- package/lib/syntax/kotlin/KotlinBoxedPrimitive.js +17 -0
- package/lib/syntax/kotlin/KotlinCxxBridgedType.d.ts +19 -0
- package/lib/syntax/kotlin/KotlinCxxBridgedType.js +893 -0
- package/lib/syntax/kotlin/KotlinEnum.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinEnum.js +113 -0
- package/lib/syntax/kotlin/KotlinFunction.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinFunction.js +256 -0
- package/lib/syntax/kotlin/KotlinHybridObject.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinHybridObject.js +177 -0
- package/lib/syntax/kotlin/KotlinHybridObjectRegistration.d.ts +17 -0
- package/lib/syntax/kotlin/KotlinHybridObjectRegistration.js +26 -0
- package/lib/syntax/kotlin/KotlinStruct.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinStruct.js +172 -0
- package/lib/syntax/kotlin/KotlinVariant.d.ts +3 -0
- package/lib/syntax/kotlin/KotlinVariant.js +191 -0
- package/lib/syntax/swift/SwiftCxxBridgedType.d.ts +21 -0
- package/lib/syntax/swift/SwiftCxxBridgedType.js +819 -0
- package/lib/syntax/swift/SwiftCxxTypeHelper.d.ts +17 -0
- package/lib/syntax/swift/SwiftCxxTypeHelper.js +613 -0
- package/lib/syntax/swift/SwiftEnum.d.ts +3 -0
- package/lib/syntax/swift/SwiftEnum.js +52 -0
- package/lib/syntax/swift/SwiftFunction.d.ts +3 -0
- package/lib/syntax/swift/SwiftFunction.js +83 -0
- package/lib/syntax/swift/SwiftHybridObject.d.ts +3 -0
- package/lib/syntax/swift/SwiftHybridObject.js +103 -0
- package/lib/syntax/swift/SwiftHybridObjectBridge.d.ts +11 -0
- package/lib/syntax/swift/SwiftHybridObjectBridge.js +451 -0
- package/lib/syntax/swift/SwiftHybridObjectRegistration.d.ts +19 -0
- package/lib/syntax/swift/SwiftHybridObjectRegistration.js +42 -0
- package/lib/syntax/swift/SwiftStruct.d.ts +3 -0
- package/lib/syntax/swift/SwiftStruct.js +75 -0
- package/lib/syntax/swift/SwiftVariant.d.ts +3 -0
- package/lib/syntax/swift/SwiftVariant.js +58 -0
- package/lib/syntax/types/ArrayBufferType.d.ts +10 -0
- package/lib/syntax/types/ArrayBufferType.js +37 -0
- package/lib/syntax/types/ArrayType.d.ts +12 -0
- package/lib/syntax/types/ArrayType.js +52 -0
- package/lib/syntax/types/BigIntType.d.ts +10 -0
- package/lib/syntax/types/BigIntType.js +27 -0
- package/lib/syntax/types/BooleanType.d.ts +10 -0
- package/lib/syntax/types/BooleanType.js +27 -0
- package/lib/syntax/types/CustomType.d.ts +14 -0
- package/lib/syntax/types/CustomType.js +36 -0
- package/lib/syntax/types/DateType.d.ts +10 -0
- package/lib/syntax/types/DateType.js +35 -0
- package/lib/syntax/types/EnumType.d.ts +23 -0
- package/lib/syntax/types/EnumType.js +101 -0
- package/lib/syntax/types/ErrorType.d.ts +11 -0
- package/lib/syntax/types/ErrorType.js +37 -0
- package/lib/syntax/types/FunctionType.d.ts +29 -0
- package/lib/syntax/types/FunctionType.js +147 -0
- package/lib/syntax/types/HybridObjectBaseType.d.ts +11 -0
- package/lib/syntax/types/HybridObjectBaseType.js +38 -0
- package/lib/syntax/types/HybridObjectType.d.ts +23 -0
- package/lib/syntax/types/HybridObjectType.js +131 -0
- package/lib/syntax/types/MapType.d.ts +10 -0
- package/lib/syntax/types/MapType.js +37 -0
- package/lib/syntax/types/NamedWrappingType.d.ts +14 -0
- package/lib/syntax/types/NamedWrappingType.js +27 -0
- package/lib/syntax/types/NullType.d.ts +10 -0
- package/lib/syntax/types/NullType.js +23 -0
- package/lib/syntax/types/NumberType.d.ts +10 -0
- package/lib/syntax/types/NumberType.js +27 -0
- package/lib/syntax/types/OptionalType.d.ts +13 -0
- package/lib/syntax/types/OptionalType.js +59 -0
- package/lib/syntax/types/PromiseType.d.ts +16 -0
- package/lib/syntax/types/PromiseType.js +62 -0
- package/lib/syntax/types/RecordType.d.ts +13 -0
- package/lib/syntax/types/RecordType.js +47 -0
- package/lib/syntax/types/ResultWrappingType.d.ts +13 -0
- package/lib/syntax/types/ResultWrappingType.js +44 -0
- package/lib/syntax/types/StringType.d.ts +10 -0
- package/lib/syntax/types/StringType.js +35 -0
- package/lib/syntax/types/StructType.d.ts +14 -0
- package/lib/syntax/types/StructType.js +61 -0
- package/lib/syntax/types/TupleType.d.ts +12 -0
- package/lib/syntax/types/TupleType.js +39 -0
- package/lib/syntax/types/Type.d.ts +55 -0
- package/lib/syntax/types/Type.js +1 -0
- package/lib/syntax/types/VariantType.d.ts +19 -0
- package/lib/syntax/types/VariantType.js +75 -0
- package/lib/syntax/types/VoidType.d.ts +10 -0
- package/lib/syntax/types/VoidType.js +27 -0
- package/lib/syntax/types/getTypeAs.d.ts +2 -0
- package/lib/syntax/types/getTypeAs.js +12 -0
- package/lib/utils.d.ts +22 -0
- package/lib/utils.js +126 -0
- package/lib/views/CppHybridViewComponent.d.ts +14 -0
- package/lib/views/CppHybridViewComponent.js +254 -0
- package/lib/views/createHostComponentJs.d.ts +3 -0
- package/lib/views/createHostComponentJs.js +27 -0
- package/lib/views/kotlin/KotlinHybridViewManager.d.ts +3 -0
- package/lib/views/kotlin/KotlinHybridViewManager.js +229 -0
- package/lib/views/swift/SwiftHybridViewManager.d.ts +3 -0
- package/lib/views/swift/SwiftHybridViewManager.js +131 -0
- package/lib/writeFile.d.ts +5 -0
- package/lib/writeFile.js +19 -0
- package/package.json +58 -29
- package/src/Logger.ts +63 -0
- package/src/autolinking/Autolinking.ts +9 -0
- package/src/autolinking/android/createCMakeExtension.ts +123 -0
- package/src/autolinking/android/createGradleExtension.ts +43 -0
- package/src/autolinking/android/createHybridObjectInitializer.ts +174 -0
- package/src/autolinking/createAndroidAutolinking.ts +28 -0
- package/src/autolinking/createIOSAutolinking.ts +24 -0
- package/src/autolinking/ios/createHybridObjectInitializer.ts +112 -0
- package/src/autolinking/ios/createPodspecRubyExtension.ts +76 -0
- package/src/autolinking/ios/createSwiftCxxBridge.ts +137 -0
- package/src/autolinking/ios/createSwiftUmbrellaHeader.ts +90 -0
- package/src/config/NitroConfig.ts +139 -0
- package/src/config/NitroUserConfig.ts +105 -0
- package/src/config/getConfig.ts +91 -0
- package/src/createGitAttributes.ts +15 -0
- package/src/createPlatformSpec.ts +176 -0
- package/src/getFiles.ts +31 -0
- package/src/getPlatformSpecs.ts +202 -0
- package/src/index.ts +146 -0
- package/src/init.ts +186 -0
- package/src/nitrogen.ts +246 -0
- package/src/prettifyDirectory.ts +32 -0
- package/src/syntax/BridgedType.ts +59 -0
- package/src/syntax/CodeNode.ts +24 -0
- package/src/syntax/HybridObjectSpec.ts +14 -0
- package/src/syntax/Method.ts +154 -0
- package/src/syntax/Parameter.ts +81 -0
- package/src/syntax/Property.ts +203 -0
- package/src/syntax/SourceFile.ts +80 -0
- package/src/syntax/c++/CppEnum.ts +128 -0
- package/src/syntax/c++/CppHybridObject.ts +165 -0
- package/src/syntax/c++/CppHybridObjectRegistration.ts +39 -0
- package/src/syntax/c++/CppStruct.ts +129 -0
- package/src/syntax/c++/CppUnion.ts +105 -0
- package/src/syntax/c++/getForwardDeclaration.ts +19 -0
- package/src/syntax/c++/includeNitroHeader.ts +40 -0
- package/src/syntax/createType.ts +365 -0
- package/src/syntax/getAllTypes.ts +18 -0
- package/src/syntax/getCustomTypeConfig.ts +71 -0
- package/src/syntax/getHybridObjectName.ts +48 -0
- package/src/syntax/getInterfaceProperties.ts +21 -0
- package/src/syntax/getReferencedTypes.ts +57 -0
- package/src/syntax/helpers.ts +79 -0
- package/src/syntax/isCoreType.ts +60 -0
- package/src/syntax/kotlin/FbjniHybridObject.ts +313 -0
- package/src/syntax/kotlin/JNINativeRegistrations.ts +19 -0
- package/src/syntax/kotlin/KotlinBoxedPrimitive.ts +19 -0
- package/src/syntax/kotlin/KotlinCxxBridgedType.ts +942 -0
- package/src/syntax/kotlin/KotlinEnum.ts +130 -0
- package/src/syntax/kotlin/KotlinFunction.ts +277 -0
- package/src/syntax/kotlin/KotlinHybridObject.ts +205 -0
- package/src/syntax/kotlin/KotlinHybridObjectRegistration.ts +51 -0
- package/src/syntax/kotlin/KotlinStruct.ts +198 -0
- package/src/syntax/kotlin/KotlinVariant.ts +212 -0
- package/src/syntax/swift/SwiftCxxBridgedType.ts +874 -0
- package/src/syntax/swift/SwiftCxxTypeHelper.ts +674 -0
- package/src/syntax/swift/SwiftEnum.ts +65 -0
- package/src/syntax/swift/SwiftFunction.ts +91 -0
- package/src/syntax/swift/SwiftHybridObject.ts +121 -0
- package/src/syntax/swift/SwiftHybridObjectBridge.ts +522 -0
- package/src/syntax/swift/SwiftHybridObjectRegistration.ts +75 -0
- package/src/syntax/swift/SwiftStruct.ts +85 -0
- package/src/syntax/swift/SwiftVariant.ts +67 -0
- package/src/syntax/types/ArrayBufferType.ts +49 -0
- package/src/syntax/types/ArrayType.ts +62 -0
- package/src/syntax/types/BigIntType.ts +35 -0
- package/src/syntax/types/BooleanType.ts +35 -0
- package/src/syntax/types/CustomType.ts +47 -0
- package/src/syntax/types/DateType.ts +43 -0
- package/src/syntax/types/EnumType.ts +130 -0
- package/src/syntax/types/ErrorType.ts +44 -0
- package/src/syntax/types/FunctionType.ts +167 -0
- package/src/syntax/types/HybridObjectBaseType.ts +54 -0
- package/src/syntax/types/HybridObjectType.ts +198 -0
- package/src/syntax/types/MapType.ts +49 -0
- package/src/syntax/types/NamedWrappingType.ts +33 -0
- package/src/syntax/types/NullType.ts +30 -0
- package/src/syntax/types/NumberType.ts +34 -0
- package/src/syntax/types/OptionalType.ts +66 -0
- package/src/syntax/types/PromiseType.ts +72 -0
- package/src/syntax/types/RecordType.ts +56 -0
- package/src/syntax/types/ResultWrappingType.ts +53 -0
- package/src/syntax/types/StringType.ts +44 -0
- package/src/syntax/types/StructType.ts +83 -0
- package/src/syntax/types/TupleType.ts +53 -0
- package/src/syntax/types/Type.ts +82 -0
- package/src/syntax/types/VariantType.ts +92 -0
- package/src/syntax/types/VoidType.ts +34 -0
- package/src/syntax/types/getTypeAs.ts +15 -0
- package/src/utils.ts +162 -0
- package/src/views/CppHybridViewComponent.ts +301 -0
- package/src/views/createHostComponentJs.ts +34 -0
- package/src/views/kotlin/KotlinHybridViewManager.ts +258 -0
- package/src/views/swift/SwiftHybridViewManager.ts +153 -0
- package/src/writeFile.ts +27 -0
- package/.jshintignore +0 -6
- package/.jshintrc +0 -3
- package/.npmignore +0 -3
- package/.travis.yml +0 -13
- package/LICENSE +0 -13
- package/browser/nitrogen-min.js +0 -3
- package/browser/nitrogen.js +0 -6369
- package/lib/apiKey.js +0 -67
- package/lib/blob.js +0 -57
- package/lib/commandManager.js +0 -350
- package/lib/device.js +0 -19
- package/lib/memoryStore.js +0 -24
- package/lib/message.js +0 -298
- package/lib/permission.js +0 -121
- package/lib/principal.js +0 -330
- package/lib/service.js +0 -349
- package/lib/session.js +0 -494
- package/lib/user.js +0 -20
- package/publish +0 -2
- package/scripts/build-documentation +0 -4
- package/scripts/build-module +0 -27
- package/scripts/module.js +0 -12
- package/scripts/postamble.js +0 -1
- package/scripts/preamble.js +0 -2
- package/scripts/run-test-server +0 -9
- package/test/config.js +0 -12
- package/test/fixtures/images/image.jpg +0 -0
- package/test/fixtures/images/motion0.jpg +0 -0
- package/test/fixtures/images/motion1.jpg +0 -0
- package/test/fixtures/images/motion2.jpg +0 -0
- package/test/fixtures/index.js +0 -76
- package/test/main.js +0 -5
- package/test/memoryStore.js +0 -22
- package/test/mocha.opts +0 -3
- package/test/units/apiKey.js +0 -46
- package/test/units/blob.js +0 -35
- package/test/units/commandManager.js +0 -67
- package/test/units/device.js +0 -26
- package/test/units/heartbeat.js +0 -28
- package/test/units/message.js +0 -79
- package/test/units/permissions.js +0 -43
- package/test/units/principal.js +0 -116
- package/test/units/service.js +0 -92
- package/test/units/session.js +0 -97
- package/test/units/user.js +0 -48
- package/yuidoc.json +0 -8
package/lib/principal.js
DELETED
|
@@ -1,330 +0,0 @@
|
|
|
1
|
-
var crypto = require('crypto')
|
|
2
|
-
, request = require('request');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @class Principal
|
|
6
|
-
* Test
|
|
7
|
-
**/
|
|
8
|
-
|
|
9
|
-
function Principal(json) {
|
|
10
|
-
this.updateAttributes(json);
|
|
11
|
-
|
|
12
|
-
if (!this.nickname) this.nickname = 'default';
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* A Principal in Nitrogen represents entities with authentication privileges in Nitrogen. Applications, devices,
|
|
17
|
-
* and services are all examples of principals.
|
|
18
|
-
**/
|
|
19
|
-
|
|
20
|
-
Principal.DEVICE_SECRET_BYTES = 128;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @method authenticate
|
|
24
|
-
* @async
|
|
25
|
-
*
|
|
26
|
-
* Authenticate this principal with the service. The mechanism used to authenticate depends on
|
|
27
|
-
* the type of principal. For users, an email and password is used. For all other principals,
|
|
28
|
-
* public key auth will be used.
|
|
29
|
-
*
|
|
30
|
-
* @param {Object} config The config for the Nitrogen service to auth against.
|
|
31
|
-
* @param {Function} callback Callback function of the form f(err, principal, accessToken).
|
|
32
|
-
**/
|
|
33
|
-
|
|
34
|
-
Principal.prototype.authenticate = function(config, callback) {
|
|
35
|
-
var self = this;
|
|
36
|
-
|
|
37
|
-
var authBody = { type: this.type, id: this.id };
|
|
38
|
-
var headers = {};
|
|
39
|
-
|
|
40
|
-
if (this.is('user')) {
|
|
41
|
-
authBody.email = this.email;
|
|
42
|
-
authBody.password = this.password;
|
|
43
|
-
} else {
|
|
44
|
-
authBody.secret = this.secret;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
var path;
|
|
48
|
-
if (this.is('user')) {
|
|
49
|
-
path = "/user/auth";
|
|
50
|
-
} else {
|
|
51
|
-
path = "/secret/auth";
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
request.post({ url: config.endpoints.principals + path, headers: headers, json: authBody }, function(err, resp, body) {
|
|
55
|
-
if (err) return callback(err);
|
|
56
|
-
if (resp.statusCode !== 200) {
|
|
57
|
-
return callback(JSON.stringify(body.error) || resp.statusCode);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
self.updateAttributes(body.principal);
|
|
61
|
-
|
|
62
|
-
return callback(null, self, body.accessToken);
|
|
63
|
-
});
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Fetch an accessToken for the passed principal.
|
|
68
|
-
*
|
|
69
|
-
* @method accessTokenFor
|
|
70
|
-
* @async
|
|
71
|
-
* @param {Object} session The session with a Nitrogen service to make this request under.
|
|
72
|
-
* @param {Object} options Options for the access token: 'expires': the date this access token should expire.
|
|
73
|
-
* @param {Function} callback Callback function of the form f(err, messages).
|
|
74
|
-
**/
|
|
75
|
-
|
|
76
|
-
Principal.accessTokenFor = function(session, principalId, options, callback) {
|
|
77
|
-
if (!session) return callback('no session passed to Principal.accessTokenFor');
|
|
78
|
-
if (!callback || typeof(callback) !== 'function') return callback('no callback passed to to Principal.accessTokenFor.');
|
|
79
|
-
|
|
80
|
-
var accessTokenUrl = session.service.config.endpoints.principals + "/accesstoken";
|
|
81
|
-
|
|
82
|
-
options.principal_id = principalId;
|
|
83
|
-
|
|
84
|
-
session.post({
|
|
85
|
-
url: accessTokenUrl,
|
|
86
|
-
json: options
|
|
87
|
-
}, function(err, resp, body) {
|
|
88
|
-
if (err) return callback(err);
|
|
89
|
-
|
|
90
|
-
callback(null, body.accessToken);
|
|
91
|
-
});
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Create a new principal with the service. For user principal types, this principal must have a email, password, and
|
|
96
|
-
* name.
|
|
97
|
-
*
|
|
98
|
-
* @method create
|
|
99
|
-
* @async
|
|
100
|
-
* @param {Object} config The config data for the Nitrogen service.
|
|
101
|
-
* @param {Function} callback Callback function of the form f(err, principal, accessToken).
|
|
102
|
-
**/
|
|
103
|
-
|
|
104
|
-
Principal.prototype.create = function(config, callback) {
|
|
105
|
-
var self = this;
|
|
106
|
-
|
|
107
|
-
crypto.randomBytes(Principal.DEVICE_SECRET_BYTES, function(err, secretBuf) {
|
|
108
|
-
if (err) return callback(err);
|
|
109
|
-
|
|
110
|
-
var secretBase64 = secretBuf.toString('base64');
|
|
111
|
-
|
|
112
|
-
if (self.is('user') && (!self.email || !self.password || !self.name)) {
|
|
113
|
-
return callback(new Error("Please provide your full name, email, and password to create an account."));
|
|
114
|
-
} else if (!self.is('user')) {
|
|
115
|
-
if (!self.api_key && config.api_key) self.api_key = config.api_key;
|
|
116
|
-
if (!self.api_key) return callback(new Error('Parameter api_key required for principal.create'));
|
|
117
|
-
|
|
118
|
-
self.secret = secretBase64;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
var principalObject = JSON.parse(JSON.stringify(self));
|
|
122
|
-
|
|
123
|
-
request.post({
|
|
124
|
-
url: config.endpoints.principals,
|
|
125
|
-
json: principalObject
|
|
126
|
-
}, function(err, resp, body) {
|
|
127
|
-
if (err) return callback(err);
|
|
128
|
-
if (resp.statusCode !== 200) return callback(body.error || resp.statusCode);
|
|
129
|
-
|
|
130
|
-
// reinject the secret
|
|
131
|
-
body.principal.secret = secretBase64;
|
|
132
|
-
|
|
133
|
-
self.updateAttributes(body.principal);
|
|
134
|
-
|
|
135
|
-
return callback(null, self, body.accessToken);
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Find principals filtered by the passed query and limited to and sorted by the passed options.
|
|
142
|
-
*
|
|
143
|
-
* @method find
|
|
144
|
-
* @param {Object} session An open session with a Nitrogen service.
|
|
145
|
-
* @param {Object} query A query filter for the principals defined using MongoDB's query format.
|
|
146
|
-
* @param {Object} options Options for the query: 'limit': maximum number of results to be returned. 'sort': The field that the results should be sorted on, 'dir': The direction that the results should be sorted. 'skip': The number of results that should be skipped before pulling results.
|
|
147
|
-
* @param {Function} callback Callback function of the form f(err, principals).
|
|
148
|
-
**/
|
|
149
|
-
|
|
150
|
-
Principal.find = function(session, query, options, callback) {
|
|
151
|
-
if (!session) return callback(new Error('Principal.findById requires active session.'));
|
|
152
|
-
|
|
153
|
-
var principalsUrl = session.service.config.endpoints.principals;
|
|
154
|
-
session.get({
|
|
155
|
-
url: principalsUrl,
|
|
156
|
-
query: query,
|
|
157
|
-
queryOptions: options,
|
|
158
|
-
json: true
|
|
159
|
-
}, function(err, resp, body) {
|
|
160
|
-
if (err) return callback(err);
|
|
161
|
-
|
|
162
|
-
var principals = body.principals.map(function(principal) {
|
|
163
|
-
return new Principal(principal);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
callback(null, principals);
|
|
167
|
-
});
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Find a principal by id.
|
|
172
|
-
*
|
|
173
|
-
* @method findById
|
|
174
|
-
* @param {Object} session An open session with a Nitrogen service.
|
|
175
|
-
* @param {String} principalId The principal id to search for.
|
|
176
|
-
* @param {Function} callback Callback function of the form f(err, principal, accessToken).
|
|
177
|
-
**/
|
|
178
|
-
|
|
179
|
-
Principal.findById = function(session, principalId, callback) {
|
|
180
|
-
if (!session) return callback(new Error('Principal.findById requires active session.'));
|
|
181
|
-
|
|
182
|
-
var principalsUrl = session.service.config.endpoints.principals + "/" + principalId;
|
|
183
|
-
session.get({ url: principalsUrl, json: true }, function(err, resp, body) {
|
|
184
|
-
if (err) return callback(err);
|
|
185
|
-
|
|
186
|
-
callback(null, new Principal(body.principal));
|
|
187
|
-
});
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Impersonate a principal using another principal's session. This is used by the service to
|
|
192
|
-
* impersonate a particular principal for application execution.
|
|
193
|
-
*
|
|
194
|
-
* @method impersonate
|
|
195
|
-
* @param {Object} session An open session with a Nitrogen service.
|
|
196
|
-
* @param {String} principalId The principal id to impersonate.
|
|
197
|
-
* @param {Function} callback Callback function of the form f(err, principal, accessToken).
|
|
198
|
-
**/
|
|
199
|
-
|
|
200
|
-
Principal.impersonate = function(session, principalId, callback) {
|
|
201
|
-
var self = this;
|
|
202
|
-
var impersonateUrl = session.service.config.endpoints.principals + "/impersonate";
|
|
203
|
-
session.post({ url: impersonateUrl, json: { id: principalId } }, function(err, resp, body) {
|
|
204
|
-
if (err) return callback(err);
|
|
205
|
-
if (resp.statusCode != 200) return callback(body.error || resp.statusCode);
|
|
206
|
-
|
|
207
|
-
var receivedPrincipal = new Principal(body.principal);
|
|
208
|
-
|
|
209
|
-
// preserve the nickname for storage
|
|
210
|
-
receivedPrincipal.nickname = self.nickname;
|
|
211
|
-
|
|
212
|
-
return callback(null, receivedPrincipal, body.accessToken);
|
|
213
|
-
});
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Delete this principal from the service.
|
|
218
|
-
*
|
|
219
|
-
* @method remove
|
|
220
|
-
* @param {Object} session An open session with a Nitrogen service.
|
|
221
|
-
* @param {Function} callback Callback function of the form f(err).
|
|
222
|
-
**/
|
|
223
|
-
|
|
224
|
-
Principal.prototype.remove = function(session, callback) {
|
|
225
|
-
var self = this;
|
|
226
|
-
|
|
227
|
-
session.remove({ url: session.service.config.endpoints.principals + "/" + this.id }, function(err) {
|
|
228
|
-
if (err) return callback(err);
|
|
229
|
-
|
|
230
|
-
session.service.clearCredentials(self);
|
|
231
|
-
|
|
232
|
-
return callback();
|
|
233
|
-
});
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Resume a session using a stored accessToken (attached to this principal).
|
|
238
|
-
*
|
|
239
|
-
* @method resume
|
|
240
|
-
* @param {Object} session An open session with a Nitrogen service.
|
|
241
|
-
* @param {Function} callback Callback function of the form f(err, principal, accessToken).
|
|
242
|
-
**/
|
|
243
|
-
|
|
244
|
-
Principal.prototype.resume = function(config, callback) {
|
|
245
|
-
// Just create session, handle failure on first usage if accessToken is invalid.
|
|
246
|
-
|
|
247
|
-
return callback(null, this, this.accessToken);
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Save this principal to the service.
|
|
252
|
-
*
|
|
253
|
-
* @method save
|
|
254
|
-
* @param {Object} session An open session with a Nitrogen service.
|
|
255
|
-
* @param {Function} callback Callback function of the form f(err, principal).
|
|
256
|
-
**/
|
|
257
|
-
|
|
258
|
-
Principal.prototype.save = function(session, callback) {
|
|
259
|
-
if (!this.id) return callback("Principal must have id to be saved.");
|
|
260
|
-
var self = this;
|
|
261
|
-
|
|
262
|
-
Principal.update(session, this.id, this, function(err, resp, body) {
|
|
263
|
-
if (err) return callback(err);
|
|
264
|
-
if (resp.statusCode != 200) return callback(resp.statusCode, null);
|
|
265
|
-
|
|
266
|
-
self.updateAttributes(body.principal);
|
|
267
|
-
|
|
268
|
-
if (callback) callback(null, self);
|
|
269
|
-
});
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* Report the status of this principal. Principal subclasses should override this method if there
|
|
274
|
-
* is meaningful status that they can provide. This status is included in the principal's heartbeat
|
|
275
|
-
* to the Nitrogen service.
|
|
276
|
-
*
|
|
277
|
-
* By default this implementation simply makes a no status callback.
|
|
278
|
-
*
|
|
279
|
-
* @method status
|
|
280
|
-
* @param {Function} callback Callback function of the form f(err, status).
|
|
281
|
-
**/
|
|
282
|
-
|
|
283
|
-
Principal.prototype.status = function(callback) {
|
|
284
|
-
// nop by default, principal subclasses should override if there is a meaningful status they can provide.
|
|
285
|
-
callback(null, {});
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
Principal.prototype.toStoreId = function() {
|
|
289
|
-
if (!this.nickname) console.log("WARNING: nickname is not defined for device.");
|
|
290
|
-
|
|
291
|
-
return "principal." + this.nickname;
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
Principal.prototype.toStoreObject = function() {
|
|
295
|
-
return {
|
|
296
|
-
id: this.id,
|
|
297
|
-
api_key: this.api_key,
|
|
298
|
-
email: this.email,
|
|
299
|
-
type: this.type,
|
|
300
|
-
public_key: this.public_key,
|
|
301
|
-
private_key: this.private_key,
|
|
302
|
-
secret: this.secret
|
|
303
|
-
};
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* Performs a partial update of a principal.
|
|
308
|
-
*
|
|
309
|
-
* @method update
|
|
310
|
-
* @param {Object} session An open session with a Nitrogen service.
|
|
311
|
-
* @param {Object} id The id of the principal to update.
|
|
312
|
-
* @param {Object} updates The update to apply to this principal.
|
|
313
|
-
* @param {Function} callback Callback function of the form f(err, resp, body).
|
|
314
|
-
**/
|
|
315
|
-
|
|
316
|
-
Principal.update = function(session, id, updates, callback) {
|
|
317
|
-
session.put({ url: session.service.config.endpoints.principals + "/" + id, json: updates }, callback);
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
Principal.prototype.updateAttributes = function(attributes) {
|
|
321
|
-
for(var key in attributes) {
|
|
322
|
-
this[key] = attributes[key];
|
|
323
|
-
}
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
Principal.prototype.is = function(type) {
|
|
327
|
-
return this.type === type;
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
module.exports = Principal;
|
package/lib/service.js
DELETED
|
@@ -1,349 +0,0 @@
|
|
|
1
|
-
var request = require('request')
|
|
2
|
-
, Principal = require('./principal')
|
|
3
|
-
, Session = require('./session')
|
|
4
|
-
, MemoryStore = require('./memoryStore');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A Service is a Nitrogen endpoint that a Session is established against for interactions with
|
|
8
|
-
* it. How that Session is established depends on the Principal type. For device and service
|
|
9
|
-
* principals, authentication is done based on a shared secret between the device and the
|
|
10
|
-
* service. For user principals, authentication is via email and password. Sessions can also
|
|
11
|
-
* be resumed if the principal has stored an authToken. The Service object is also responsible
|
|
12
|
-
* for querying the headwaiter endpoint to fetch the service endpoints that this Session should
|
|
13
|
-
* use.
|
|
14
|
-
*
|
|
15
|
-
* @class Service
|
|
16
|
-
* @namespace nitrogen
|
|
17
|
-
**/
|
|
18
|
-
|
|
19
|
-
function Service(config) {
|
|
20
|
-
this.config = config || {};
|
|
21
|
-
|
|
22
|
-
if (!this.config.store) this.config.store = new MemoryStore();
|
|
23
|
-
|
|
24
|
-
this.config.host = this.config.host || 'api.nitrogen.io';
|
|
25
|
-
this.config.protocol = this.config.protocol || 'https';
|
|
26
|
-
this.config.http_port = this.config.http_port || 443;
|
|
27
|
-
this.config.max_session_lifetime = Service.MAX_SESSION_LIFETIME_DEFAULT;
|
|
28
|
-
|
|
29
|
-
this.config.base_url = this.config.protocol + "://" + this.config.host + ":" + this.config.http_port + "/api/v1";
|
|
30
|
-
|
|
31
|
-
if (!this.config.endpoints) this.config.endpoints = {};
|
|
32
|
-
|
|
33
|
-
for (var key in config.endpoints) {
|
|
34
|
-
this.config.endpoints[key] = config.endpoints[key];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (!this.config.endpoints.headwaiter) {
|
|
38
|
-
this.config.endpoints.headwaiter = this.config.base_url + "/headwaiter";
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Need this for password reset. Can be overridden by response from headwaiter.
|
|
42
|
-
if (!this.config.endpoints.principals) {
|
|
43
|
-
this.config.endpoints.principals = this.config.base_url + "/principals";
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
this.store = config.store;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
Service.BACKOFF_RATE_MILLIS = 500;
|
|
50
|
-
Service.MAXIMUM_BACKOFF_STEPS = 7;
|
|
51
|
-
|
|
52
|
-
Service.MAX_SESSION_LIFETIME_DEFAULT = 24 * 60 * 60 * 1000; // ms
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Authenticate this principal with the Nitrogen service. The mechanism used to authenticate
|
|
56
|
-
* depends on the type of principal. For users, an email and password is used. For other principals
|
|
57
|
-
* public key encryption is verify a signed nonce value for authentication.
|
|
58
|
-
*
|
|
59
|
-
* @method authenticate
|
|
60
|
-
* @async
|
|
61
|
-
* @param {Object} principal The principal to authenticate with this service. The principal should include the email/password for a user principal or the private_key for other principal types.
|
|
62
|
-
* @param {Function} callback Callback function with signature f(err, session, principal).
|
|
63
|
-
**/
|
|
64
|
-
|
|
65
|
-
Service.prototype.authenticate = function(principal, callback) {
|
|
66
|
-
this.authenticateSession(principal, principal.authenticate, callback);
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Creates the principal with this service.
|
|
71
|
-
*
|
|
72
|
-
* @method create
|
|
73
|
-
* @async
|
|
74
|
-
* @param {Object} principal The principal to create with this service. It should include email/password for user principal types.
|
|
75
|
-
* @param {Function} callback Callback function with signature f(err, session, principal).
|
|
76
|
-
**/
|
|
77
|
-
|
|
78
|
-
Service.prototype.create = function(principal, callback) {
|
|
79
|
-
this.authenticateSession(principal, principal.create, callback);
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Attempts to resume a session for this principal using a saved accessToken.
|
|
84
|
-
*
|
|
85
|
-
* @method resume
|
|
86
|
-
* @async
|
|
87
|
-
* @param {Object} principal The principal to resume the session with this service.
|
|
88
|
-
* @param {Function} callback Callback function with signature f(err, session, principal).
|
|
89
|
-
**/
|
|
90
|
-
|
|
91
|
-
Service.prototype.resume = function(principal, callback) {
|
|
92
|
-
if (!this.store) return callback(new Error('No store configured with service.'));
|
|
93
|
-
|
|
94
|
-
this.authenticateSession(principal, principal.resume, function(err, session, principal) {
|
|
95
|
-
if (err) return callback(err);
|
|
96
|
-
|
|
97
|
-
Principal.findById(session, principal.id, function(err, loadedPrincipal) {
|
|
98
|
-
if (err) return callback(err);
|
|
99
|
-
|
|
100
|
-
loadedPrincipal.nickname = principal.nickname;
|
|
101
|
-
return callback(err, session, loadedPrincipal);
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Connect attempts to authenicate the principal with the service. If the principal
|
|
108
|
-
* isn't provisioned with the service, it automatically creates the principal with the
|
|
109
|
-
* service.
|
|
110
|
-
*
|
|
111
|
-
* @method connect
|
|
112
|
-
* @async
|
|
113
|
-
* @param {Object} principal The principal to connect with this service.
|
|
114
|
-
* @param {Function} callback Callback function with signature f(err, session, principal).
|
|
115
|
-
**/
|
|
116
|
-
|
|
117
|
-
Service.prototype.connect = function(principal, callback) {
|
|
118
|
-
var self = this;
|
|
119
|
-
|
|
120
|
-
this.store.get(principal.toStoreId(), function(err, storedPrincipal) {
|
|
121
|
-
if (storedPrincipal) {
|
|
122
|
-
principal.updateAttributes(storedPrincipal);
|
|
123
|
-
self.restartOnFailureAuthWrapper(principal, principal.authenticate, callback);
|
|
124
|
-
} else {
|
|
125
|
-
self.restartOnFailureAuthWrapper(principal, principal.create, callback);
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
function asyncWhilst(test, iterator, callback) {
|
|
131
|
-
if (test()) {
|
|
132
|
-
iterator(function (err) {
|
|
133
|
-
if (err) {
|
|
134
|
-
return callback(err);
|
|
135
|
-
}
|
|
136
|
-
asyncWhilst(test, iterator, callback);
|
|
137
|
-
});
|
|
138
|
-
} else {
|
|
139
|
-
callback();
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
Service.prototype.restartOnFailureAuthWrapper = function(principal, initialOp, sessionCallback) {
|
|
144
|
-
var self = this;
|
|
145
|
-
var authOp = initialOp;
|
|
146
|
-
|
|
147
|
-
asyncWhilst(
|
|
148
|
-
function() { return true; },
|
|
149
|
-
function(restartCallback) {
|
|
150
|
-
self.authOpWithRetry(principal, authOp, function(err, session, principal) {
|
|
151
|
-
if (err) return sessionCallback(err);
|
|
152
|
-
|
|
153
|
-
session.onFailure(function() {
|
|
154
|
-
session.log.error('session failure: restarting');
|
|
155
|
-
session.stop();
|
|
156
|
-
|
|
157
|
-
authOp = principal.authenticate;
|
|
158
|
-
return restartCallback();
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
return sessionCallback(err, session, principal);
|
|
162
|
-
});
|
|
163
|
-
},
|
|
164
|
-
function(err) {
|
|
165
|
-
// should never get here
|
|
166
|
-
}
|
|
167
|
-
);
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
Service.prototype.authOpWithRetry = function(principal, authOperation, callback) {
|
|
171
|
-
|
|
172
|
-
var self = this;
|
|
173
|
-
var failures = 0;
|
|
174
|
-
var successful = false;
|
|
175
|
-
|
|
176
|
-
var backoffMillis = 0;
|
|
177
|
-
|
|
178
|
-
var err;
|
|
179
|
-
var session;
|
|
180
|
-
|
|
181
|
-
asyncWhilst(
|
|
182
|
-
function () { return !successful; },
|
|
183
|
-
function (retryCallback) {
|
|
184
|
-
|
|
185
|
-
if (backoffMillis > 0.0)
|
|
186
|
-
console.log('service: retrying auth request after backoff of ' + backoffMillis + ' ms.');
|
|
187
|
-
|
|
188
|
-
setTimeout(function() {
|
|
189
|
-
self.authenticateSession(principal, authOperation, function(e, s, p) {
|
|
190
|
-
successful = !e;
|
|
191
|
-
err = e;
|
|
192
|
-
|
|
193
|
-
if (!successful) {
|
|
194
|
-
console.log('service: authentication failed: ' + JSON.stringify(err));
|
|
195
|
-
|
|
196
|
-
failures += 1;
|
|
197
|
-
console.log('service: ' + failures + ' consecutive auth failures.');
|
|
198
|
-
|
|
199
|
-
var backoffAmount = Math.min(failures, Service.MAXIMUM_BACKOFF_STEPS);
|
|
200
|
-
|
|
201
|
-
backoffMillis = Math.pow(2, backoffAmount) * Service.BACKOFF_RATE_MILLIS * (1 + Math.random());
|
|
202
|
-
backoffMillis = Math.floor(backoffMillis);
|
|
203
|
-
} else {
|
|
204
|
-
session = s;
|
|
205
|
-
principal = p;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return retryCallback();
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
}, backoffMillis);
|
|
212
|
-
},
|
|
213
|
-
function (e) {
|
|
214
|
-
return callback(err, session, principal);
|
|
215
|
-
}
|
|
216
|
-
);
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Internal method to run all the common steps of authentication against a Nitrogen service.
|
|
221
|
-
*
|
|
222
|
-
* @method authenticateSession
|
|
223
|
-
* @async
|
|
224
|
-
* @private
|
|
225
|
-
* @param {Object} principal The principal to connect with this service.
|
|
226
|
-
* @param {Object} authOperation The authorization method on the principal that is used to .
|
|
227
|
-
* @param {Function} callback Callback function with signature f(err, session, principal).
|
|
228
|
-
**/
|
|
229
|
-
|
|
230
|
-
Service.prototype.authenticateSession = function(principal, authOperation, callback) {
|
|
231
|
-
var self = this;
|
|
232
|
-
|
|
233
|
-
this.configure(principal, function(err, config) {
|
|
234
|
-
if (err) return callback(err);
|
|
235
|
-
|
|
236
|
-
self.config = config;
|
|
237
|
-
|
|
238
|
-
if (self.config.log_levels && self.config.log_levels.indexOf('debug')) console.log('service: configured.');
|
|
239
|
-
|
|
240
|
-
authOperation.bind(principal)(self.config, function(err, principal, accessToken) {
|
|
241
|
-
if (err) return callback(err);
|
|
242
|
-
|
|
243
|
-
if (!principal) return callback(new Error("authentication failed: no principal returned"));
|
|
244
|
-
if (!accessToken) return callback(new Error("authentication failed: no accessToken returned"));
|
|
245
|
-
|
|
246
|
-
if (self.config.log_levels && self.config.log_levels.indexOf('debug') !== -1) console.log('principal: authenticated.');
|
|
247
|
-
|
|
248
|
-
if (principal.claim_code) {
|
|
249
|
-
console.log('This principal (' + principal.id + ') can be claimed using code: ' + principal.claim_code);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
self.store.set(principal.toStoreId(), principal.toStoreObject(), function(err) {
|
|
253
|
-
if (err) return callback(err);
|
|
254
|
-
|
|
255
|
-
var session = Session.startSession(self, principal, accessToken);
|
|
256
|
-
|
|
257
|
-
if (self.config.max_session_lifetime)
|
|
258
|
-
setInterval(function() { session.onFailure(); }, config.max_session_lifetime);
|
|
259
|
-
|
|
260
|
-
callback(null, session, principal);
|
|
261
|
-
});
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Impersonate a principal using the passed session
|
|
268
|
-
*
|
|
269
|
-
* @method impersonate
|
|
270
|
-
* @async
|
|
271
|
-
* @param {Object} session The session to use to authorize this impersonation
|
|
272
|
-
* @param {Object} principal The principal to impersonate with this service.
|
|
273
|
-
* @param {Function} callback Callback function with signature f(err, session, principal).
|
|
274
|
-
**/
|
|
275
|
-
|
|
276
|
-
Service.prototype.impersonate = function(session, principalId, callback) {
|
|
277
|
-
var self = this;
|
|
278
|
-
|
|
279
|
-
Principal.impersonate(session, principalId, function(err, impersonatedPrincipal, accessToken) {
|
|
280
|
-
if (err) return callback(err);
|
|
281
|
-
|
|
282
|
-
impersonatedPrincipal.accessToken = accessToken;
|
|
283
|
-
|
|
284
|
-
var impersonatedSession = Session.startSession(self, impersonatedPrincipal, accessToken);
|
|
285
|
-
|
|
286
|
-
// configure the impersonatedPrincipal session in case the principal has a different config.
|
|
287
|
-
self.configure(impersonatedPrincipal, function(err, config) {
|
|
288
|
-
if (err) return callback(err);
|
|
289
|
-
|
|
290
|
-
impersonatedSession.service.config = config;
|
|
291
|
-
callback(null, impersonatedSession, impersonatedPrincipal);
|
|
292
|
-
});
|
|
293
|
-
});
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Fetch the endpoint configuration for this service for this user. Before authenticating a principal, we first
|
|
298
|
-
* ask the service to return the set of endpoints that we should for this principal to talk to the Nitrogen service. Note,
|
|
299
|
-
* this might actually not be the same service, as Nitrogen may redirect clients to a different service or different endpoints.
|
|
300
|
-
*
|
|
301
|
-
* @method configure
|
|
302
|
-
* @async
|
|
303
|
-
* @private
|
|
304
|
-
* @param {Object} config The default configuration to use to connect to Nitrogen.
|
|
305
|
-
* @param {Object} principal The principal to use configure this service.
|
|
306
|
-
* @param {Function} callback Callback function with signature f(err, configuration).
|
|
307
|
-
**/
|
|
308
|
-
|
|
309
|
-
Service.prototype.configure = function(principal, callback) {
|
|
310
|
-
var self = this;
|
|
311
|
-
var headwaiter_url = this.config.endpoints.headwaiter;
|
|
312
|
-
|
|
313
|
-
if (principal) {
|
|
314
|
-
if (principal.is('user') && principal.email) {
|
|
315
|
-
headwaiter_url += "?email=" + principal.email;
|
|
316
|
-
} else if (principal.id) {
|
|
317
|
-
headwaiter_url += "?principal_id=" + principal.id;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
request.get({ url: headwaiter_url, json: true }, function(err, resp, body) {
|
|
322
|
-
if (err) return callback(err);
|
|
323
|
-
if (resp.statusCode != 200) return callback(JSON.stringify(body.error) || resp.statusCode);
|
|
324
|
-
|
|
325
|
-
for (var key in body.endpoints) {
|
|
326
|
-
self.config.endpoints[key] = body.endpoints[key];
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
self.config.nonce = body.nonce;
|
|
330
|
-
|
|
331
|
-
callback(null, self.config);
|
|
332
|
-
});
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* Clear all of the credentials for a particular principal.
|
|
337
|
-
*
|
|
338
|
-
* @method clearCredentials
|
|
339
|
-
* @private
|
|
340
|
-
* @param {Object} principal The principal to clear credentials for.
|
|
341
|
-
**/
|
|
342
|
-
|
|
343
|
-
Service.prototype.clearCredentials = function(principal) {
|
|
344
|
-
if (!this.store) return;
|
|
345
|
-
|
|
346
|
-
this.store.delete(principal.toStoreId());
|
|
347
|
-
};
|
|
348
|
-
|
|
349
|
-
module.exports = Service;
|