mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-11 14:54:33 +00:00
Adding JWT auth (#1564)
* Adding method Realm.Sync.User.authenticate() * Adding JWT provider support to Realm.Sync.User.authenticate() * Adding password provider support to Realm.Sync.User.authenticate()
This commit is contained in:
parent
1b0f6c0b21
commit
16a218ad49
@ -4,7 +4,8 @@ X.Y.Z Release notes
|
||||
* None.
|
||||
|
||||
### Enhancements
|
||||
* None.
|
||||
* [Object Server] Added method `Realm.Sync.User.authenticate` to unify authentication of users.
|
||||
* [Object Server] Added JWT authenfication (#1548).
|
||||
|
||||
### Bug fixes
|
||||
* Fix a bug where `Realm.open` could unexpectedly raise a "Realm at path ... already opened with different schema version" error.
|
||||
@ -12,7 +13,7 @@ X.Y.Z Release notes
|
||||
should help with failing token refreshes on a loaded server.
|
||||
|
||||
### Internal
|
||||
* None
|
||||
* None.
|
||||
|
||||
2.1.1 Release notes (2017-12-15)
|
||||
=============================================================
|
||||
@ -24,9 +25,9 @@ X.Y.Z Release notes
|
||||
|
||||
### Bug fixes
|
||||
* [Object Server] Fixed a bug where long reconnection happens when a proxy in front of the sync worker returns one of those.
|
||||
* Fix a bug where `Realm.open` could unexpectedly raise a "Realm at path ... already opened with different schema version" error.
|
||||
|
||||
### Internal
|
||||
* [Object Server] Updated to Realm Object Server v2.2.0 for testing.
|
||||
* Updated to Realm Sync 2.1.10 (see "Bug fixes").
|
||||
|
||||
|
||||
|
@ -2,4 +2,4 @@ PACKAGE_NAME=realm-js
|
||||
VERSION=2.1.1
|
||||
REALM_CORE_VERSION=4.0.2
|
||||
REALM_SYNC_VERSION=2.1.10
|
||||
REALM_OBJECT_SERVER_VERSION=2.0.21
|
||||
REALM_OBJECT_SERVER_VERSION=2.2.0
|
11
docs/sync.js
11
docs/sync.js
@ -178,6 +178,17 @@ class User {
|
||||
*/
|
||||
static login(server, username, password, callback) {}
|
||||
|
||||
/**
|
||||
* Authenticate a sync user with provider.
|
||||
* @param {string} server - authentication server
|
||||
* @param {string} provider - the provider (curently: 'password', and 'jwt')
|
||||
* @param {object} options - options used by provider:
|
||||
* - jwt - `token`; a JWT token
|
||||
* - password - `username` and `password`
|
||||
* @return {Promise<User>} Returns a promise with a user
|
||||
*/
|
||||
static authenticate(server, provider, options) {}
|
||||
|
||||
/**
|
||||
* Register/login a sync user using an external login provider.
|
||||
* @param {string} server - authentication server
|
||||
|
1
lib/index.d.ts
vendored
1
lib/index.d.ts
vendored
@ -290,6 +290,7 @@ declare namespace Realm.Sync {
|
||||
static registerWithProvider(server: string, options: { provider: string, providerToken: string, userInfo: any }, callback: (error: Error | null, user: User | null) => void): void;
|
||||
static registerWithProvider(server: string, options: { provider: string, providerToken: string, userInfo: any }): Promise<Realm.Sync.User>;
|
||||
|
||||
authenticate(server: string, provider: string, options: any): Promise<Realm.Sync.User>;
|
||||
logout(): void;
|
||||
openManagementRealm(): Realm;
|
||||
retrieveAccount(provider: string, username: string): Promise<Account>;
|
||||
|
@ -269,8 +269,31 @@ const staticMethods = {
|
||||
return _authenticate(this, server, json, callback);
|
||||
},
|
||||
|
||||
_refreshAccessToken: refreshAccessToken
|
||||
};
|
||||
authenticate(server, provider, options) {
|
||||
checkTypes(arguments, ['string', 'string', 'object'])
|
||||
|
||||
var json = {}
|
||||
switch (provider.toLowerCase()) {
|
||||
case 'jwt':
|
||||
json.provider = 'jwt'
|
||||
json.token = options.token;
|
||||
break
|
||||
case 'password':
|
||||
json.provider = 'password'
|
||||
json.user_info = { password: options.password }
|
||||
json.data = options.username
|
||||
break
|
||||
default:
|
||||
Object.assign(json, options)
|
||||
json.provider = provider
|
||||
}
|
||||
|
||||
return _authenticate(this, server, json)
|
||||
},
|
||||
|
||||
_refreshAccessToken: refreshAccessToken,
|
||||
};
|
||||
|
||||
|
||||
const instanceMethods = {
|
||||
openManagementRealm() {
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit d681b1fb8a8ca7a8db1ab1edc25771d984795ebe
|
||||
Subproject commit 3eb19c014fdfa0f02a03d4acf71d046d29a6dfa6
|
@ -136,6 +136,19 @@ module.exports = {
|
||||
}))
|
||||
},
|
||||
|
||||
testAuthenticateWithPassword() {
|
||||
const username = uuid();
|
||||
return Realm.Sync.User.register('http://localhost:9080', username, 'password').then((user) => {
|
||||
user.logout();
|
||||
return Realm.Sync.User.authenticate('http://localhost:9080', 'password', { username: username, password: 'password' });
|
||||
}).then((user => {
|
||||
assertIsUser(user);
|
||||
const realm = new Realm({ sync: { user: user, url: 'realm://localhost:9080/~/test' } });
|
||||
TestCase.assertInstanceOf(realm, Realm);
|
||||
realm.close();
|
||||
}))
|
||||
},
|
||||
|
||||
testLoginMissingUsername() {
|
||||
TestCase.assertThrows(() => Realm.Sync.User.login('http://localhost:9080', undefined, 'password'));
|
||||
},
|
||||
@ -174,6 +187,22 @@ module.exports = {
|
||||
});
|
||||
},
|
||||
|
||||
testAuthenticateInvalidProvider() {
|
||||
return Realm.Sync.User.authenticate('http://localhost:9080', 'FooBar', {})
|
||||
.then((user) => { Promise.reject() } )
|
||||
.catch((e) => { Promise.resolve() } )
|
||||
},
|
||||
|
||||
testAuthenticateJWT() {
|
||||
let token = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJhdXN0aW5femhlbmciLCJpc0FkbWluIjp0cnVlLCJpYXQiOjE1MTI2OTI3NDl9.klca-3wYLe5mGdVk7N7dE9YRIlB1el1Dv6BxZNAKMsJ3Ms4vBTweu4-65kVJftiMrYhmSGY6QtTzqQ-xlLH4XzPd3jYIXlPQ45lxO7PW7EkJNs9m83VdcsJmHRHQ3PRP8V_mx0f2Ks4ga3xZ9IycAQB4q5NXLei_HJk8tRRJccZ6qB5nnAoD48Qu8JOEfhO596Mdoi-QCbH51iJZjgXo4gSRZ4KKK8jU0S6twLj_lf9jehENTqHDdtsRHdyCnICcPcz4AjFrNHEvUrsPkGxXSZ2BCGgDcvsSTVgGNV7rWU4IjH4FaDssenumi50R1QcZh8kiO35s9H6MngQsEm-zApRgd0V9_L3A6Ys47_crmKbunYRsATfMNBn2fKm5tS6RXvM2RN2G_Y9AkGgh2boY42CRy7HOcHby2vQ8IoQ-fZfE5xn_YYktNlKeNiCv3_-i86lANFbmB3tcdScrbjsgO6Tfg3u71VmJ_ZW1_vyMi5vCTEysLXfHG-OA85c3o8-25vcfuX5gIpbU-nMLgPagyn5w7Uazd27uhFfwepP9OMc8jz2JTlQICInLCUdESu8aG5d1F_IPUA5NU_ryPmebqUmyaRVDS8cGChxp0gZDNSiIvaggw8N2JCDGvk-s_PSG2pFGq0f4veYyWGBTHD_iX4a0UrhB471QZplRpMwvu7o'
|
||||
return Realm.Sync.User.authenticate('http://localhost:9080', 'jwt', { token: token })
|
||||
.then((user) => {
|
||||
TestCase.assertEqual(user.identity, 'austin_zheng')
|
||||
Promise.resolve()
|
||||
})
|
||||
.catch((e) => { Promise.reject(e) } )
|
||||
},
|
||||
|
||||
testAll() {
|
||||
const all = Realm.Sync.User.all;
|
||||
TestCase.assertArrayLength(Object.keys(all), 0);
|
||||
|
Loading…
x
Reference in New Issue
Block a user