mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-10-07 22:57:07 +00:00
feat: Expose identity provider managers. (#75)
* feat: Expose identity provider managers. * refactor: Remove unnecessary provider tools. * refactor: Match AC management to other management mechanisms (e.g. Identity Providers).
This commit is contained in:
@@ -21,12 +21,12 @@ import IPFSAccessController from './ipfs.js'
|
||||
import OrbitDBAccessController from './orbitdb.js'
|
||||
|
||||
/**
|
||||
* An array of available access controller types.
|
||||
* @name types
|
||||
* An array of available access controllers.
|
||||
* @name accessControllers
|
||||
* @†ype []
|
||||
* @return [] An array of access controller types.
|
||||
* @return [] An array of access controllers.
|
||||
*/
|
||||
const types = {
|
||||
const accessControllers = {
|
||||
ipfs: IPFSAccessController,
|
||||
orbitdb: OrbitDBAccessController
|
||||
}
|
||||
@@ -36,25 +36,15 @@ const types = {
|
||||
* @param {string} type A valid access controller type.
|
||||
* @return {AccessController} The access controller module.
|
||||
*/
|
||||
const get = (type) => {
|
||||
if (!isSupported(type)) {
|
||||
const getAccessController = (type) => {
|
||||
if (!Object.keys(accessControllers).includes(type)) {
|
||||
throw new Error(`AccessController type '${type}' is not supported`)
|
||||
}
|
||||
return types[type]
|
||||
return accessControllers[type]
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the access controller exists.
|
||||
* @param {string} type A valid access controller type.
|
||||
* @return {boolean} True if the access controller exists, false otherwise.
|
||||
*/
|
||||
const isSupported = type => {
|
||||
return Object.keys(types).includes(type)
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an access controller module to the list of supported access controller
|
||||
* types.
|
||||
* Adds an access controller module to the list of supported access controller.
|
||||
* @param {AccessController} accessController A compatible access controller
|
||||
* module.
|
||||
* @throws Access controller `type` already added if the access controller is
|
||||
@@ -62,8 +52,8 @@ const isSupported = type => {
|
||||
* @throws Given AccessController class needs to implement: type if the access
|
||||
* controller module does not implement a type property.
|
||||
*/
|
||||
const add = (accessController) => {
|
||||
if (types[accessController.type]) {
|
||||
const addAccessController = (accessController) => {
|
||||
if (accessControllers[accessController.type]) {
|
||||
throw new Error(`Access controller '${accessController.type}' already added.`)
|
||||
}
|
||||
|
||||
@@ -71,21 +61,20 @@ const add = (accessController) => {
|
||||
throw new Error('Given AccessController class needs to implement: type.')
|
||||
}
|
||||
|
||||
types[accessController.type] = accessController
|
||||
accessControllers[accessController.type] = accessController
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an access controller from the types list.
|
||||
* Removes an access controller from the list.
|
||||
* @param {string} type A valid access controller type.
|
||||
*/
|
||||
const remove = type => {
|
||||
delete types[type]
|
||||
const removeAccessController = type => {
|
||||
delete accessControllers[type]
|
||||
}
|
||||
|
||||
export {
|
||||
types,
|
||||
get,
|
||||
isSupported,
|
||||
add,
|
||||
remove
|
||||
accessControllers,
|
||||
getAccessController,
|
||||
addAccessController,
|
||||
removeAccessController
|
||||
}
|
||||
|
||||
@@ -4,9 +4,8 @@
|
||||
* Identities provides a framework for generating and managing identity
|
||||
* details and providers.
|
||||
*/
|
||||
|
||||
import Identity, { isIdentity, isEqual, decodeIdentity } from './identity.js'
|
||||
import { PublicKeyIdentityProvider } from './providers/index.js'
|
||||
import { getProviderFor } from './providers/index.js'
|
||||
// import DIDIdentityProvider from './identity-providers/did.js'
|
||||
// import EthIdentityProvider from './identity-providers/ethereum.js'
|
||||
import KeyStore, { signMessage, verifyMessage } from '../key-store.js'
|
||||
@@ -14,13 +13,8 @@ import { LRUStorage, IPFSBlockStorage, MemoryStorage, ComposedStorage } from '..
|
||||
import pathJoin from '../utils/path-join.js'
|
||||
|
||||
const DefaultProviderType = 'publickey'
|
||||
const DefaultIdentityKeysPath = pathJoin('./orbitdb', 'identities')
|
||||
|
||||
const supportedTypes = {
|
||||
publickey: PublicKeyIdentityProvider
|
||||
// [DIDIdentityProvider.type]: DIDIdentityProvider,
|
||||
// [EthIdentityProvider.type]: EthIdentityProvider
|
||||
}
|
||||
const DefaultIdentityKeysPath = pathJoin('./orbitdb', 'identities')
|
||||
|
||||
/**
|
||||
* Creates an instance of Identities.
|
||||
@@ -167,68 +161,6 @@ const Identities = async ({ keystore, path, storage, ipfs } = {}) => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether an identity provider is supported.
|
||||
* @param {string} type The identity provider type.
|
||||
* @return {boolean} True if the identity provider is supported, false
|
||||
* otherwise.
|
||||
* @static
|
||||
*/
|
||||
const isProviderSupported = (type) => {
|
||||
return Object.keys(supportedTypes).includes(type)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an identity provider.
|
||||
* @param {string} type The identity provider type.
|
||||
* @return {IdentityProvider} The IdentityProvider module corresponding to
|
||||
* type.
|
||||
* @throws IdentityProvider type is not supported if the identity provider is
|
||||
* not supported.
|
||||
* @static
|
||||
*/
|
||||
const getProviderFor = (type) => {
|
||||
if (!isProviderSupported(type)) {
|
||||
throw new Error(`IdentityProvider type '${type}' is not supported`)
|
||||
}
|
||||
|
||||
return supportedTypes[type]
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an identity provider.
|
||||
* @param {IdentityProvider} IdentityProvider The identity provider to add.
|
||||
* @throws IdentityProvider must be given as an argument if no module is
|
||||
* provided.
|
||||
* @throws 'Given IdentityProvider doesn't have a field 'type' if the
|
||||
* IdentityProvider does not include a type property.
|
||||
* @static
|
||||
*/
|
||||
const addIdentityProvider = (IdentityProvider) => {
|
||||
if (!IdentityProvider) {
|
||||
throw new Error('IdentityProvider must be given as an argument')
|
||||
}
|
||||
|
||||
if (!IdentityProvider.type ||
|
||||
typeof IdentityProvider.type !== 'string') {
|
||||
throw new Error('Given IdentityProvider doesn\'t have a field \'type\'')
|
||||
}
|
||||
|
||||
supportedTypes[IdentityProvider.type] = IdentityProvider
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an identity provider.
|
||||
* @param {string} type The identity provider type.
|
||||
* @static
|
||||
*/
|
||||
const removeIdentityProvider = (type) => {
|
||||
delete supportedTypes[type]
|
||||
}
|
||||
|
||||
export {
|
||||
Identities as default,
|
||||
isProviderSupported,
|
||||
addIdentityProvider,
|
||||
removeIdentityProvider
|
||||
Identities as default
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
* @description
|
||||
* An identity.
|
||||
*/
|
||||
|
||||
import * as Block from 'multiformats/block'
|
||||
import * as dagCbor from '@ipld/dag-cbor'
|
||||
import { sha256 } from 'multiformats/hashes/sha2'
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
export {
|
||||
default as Identities,
|
||||
addIdentityProvider,
|
||||
removeIdentityProvider,
|
||||
isProviderSupported
|
||||
} from './identities.js'
|
||||
export { default as Identities } from './identities.js'
|
||||
|
||||
export {
|
||||
default as Identity,
|
||||
@@ -11,4 +6,8 @@ export {
|
||||
isEqual
|
||||
} from './identity.js'
|
||||
|
||||
export { PublicKeyIdentityProvider } from './providers/index.js'
|
||||
export {
|
||||
PublicKeyIdentityProvider,
|
||||
addIdentityProvider,
|
||||
identityProviders
|
||||
} from './providers/index.js'
|
||||
|
||||
@@ -46,7 +46,68 @@
|
||||
*
|
||||
* where my-custom-identity-provider is the custom module.
|
||||
*/
|
||||
import * as PublicKeyIdentityProvider from './publickey.js'
|
||||
|
||||
const identityProviders = {
|
||||
publickey: PublicKeyIdentityProvider
|
||||
// [DIDIdentityProvider.type]: DIDIdentityProvider,
|
||||
// [EthIdentityProvider.type]: EthIdentityProvider
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether an identity provider is supported.
|
||||
* @param {string} type The identity provider type.
|
||||
* @return {boolean} True if the identity provider is supported, false
|
||||
* otherwise.
|
||||
* @static
|
||||
*/
|
||||
const isProviderSupported = (type) => {
|
||||
return Object.keys(identityProviders).includes(type)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an identity provider.
|
||||
* @param {string} type The identity provider type.
|
||||
* @return {IdentityProvider} The IdentityProvider module corresponding to
|
||||
* type.
|
||||
* @throws IdentityProvider type is not supported if the identity provider is
|
||||
* not supported.
|
||||
* @static
|
||||
*/
|
||||
const getProviderFor = (type) => {
|
||||
if (!isProviderSupported(type)) {
|
||||
throw new Error(`IdentityProvider type '${type}' is not supported`)
|
||||
}
|
||||
|
||||
return identityProviders[type]
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an identity provider.
|
||||
* @param {IdentityProvider} IdentityProvider The identity provider to add.
|
||||
* @throws IdentityProvider must be given as an argument if no module is
|
||||
* provided.
|
||||
* @throws 'Given IdentityProvider doesn't have a field 'type' if the
|
||||
* IdentityProvider does not include a type property.
|
||||
* @static
|
||||
*/
|
||||
const addIdentityProvider = (IdentityProvider) => {
|
||||
if (!IdentityProvider) {
|
||||
throw new Error('IdentityProvider must be given as an argument')
|
||||
}
|
||||
|
||||
if (!IdentityProvider.type ||
|
||||
typeof IdentityProvider.type !== 'string') {
|
||||
throw new Error('Given IdentityProvider doesn\'t have a field \'type\'')
|
||||
}
|
||||
|
||||
if (identityProviders[IdentityProvider.type]) {
|
||||
throw new Error(`Type already added: ${IdentityProvider.type}`)
|
||||
}
|
||||
|
||||
identityProviders[IdentityProvider.type] = IdentityProvider
|
||||
}
|
||||
|
||||
// export { default as DIDIdentityProvider } from './did.js'
|
||||
// export { default as EthIdentityProvider } from './ethereum.js'
|
||||
export * as PublicKeyIdentityProvider from './publickey.js'
|
||||
export { identityProviders, addIdentityProvider, getProviderFor, PublicKeyIdentityProvider }
|
||||
|
||||
41
src/index.js
41
src/index.js
@@ -1,8 +1,39 @@
|
||||
export { default as OrbitDB } from './orbitdb.js'
|
||||
export { databaseTypes, addDatabaseType } from './orbitdb.js'
|
||||
export { default as OrbitDBAddress, isValidAddress, parseAddress } from './address.js'
|
||||
export {
|
||||
default as OrbitDB,
|
||||
databaseTypes,
|
||||
addDatabaseType
|
||||
} from './orbitdb.js'
|
||||
|
||||
export {
|
||||
default as OrbitDBAddress,
|
||||
isValidAddress,
|
||||
parseAddress
|
||||
} from './address.js'
|
||||
|
||||
export { Log, Entry, DefaultAccessController } from './oplog/index.js'
|
||||
|
||||
export { default as Database } from './database.js'
|
||||
|
||||
export { default as KeyStore } from './key-store.js'
|
||||
export { Identities, isIdentity } from './identities/index.js'
|
||||
export { IPFSBlockStorage, LevelStorage, LRUStorage, MemoryStorage, ComposedStorage } from './storage/index.js'
|
||||
|
||||
export {
|
||||
addAccessController,
|
||||
removeAccessController,
|
||||
getAccessController,
|
||||
accessControllers
|
||||
} from './access-controllers/index.js'
|
||||
|
||||
export {
|
||||
Identities,
|
||||
isIdentity,
|
||||
identityProviders,
|
||||
addIdentityProvider
|
||||
} from './identities/index.js'
|
||||
|
||||
export {
|
||||
IPFSBlockStorage,
|
||||
LevelStorage,
|
||||
LRUStorage,
|
||||
MemoryStorage,
|
||||
ComposedStorage
|
||||
} from './storage/index.js'
|
||||
|
||||
@@ -35,7 +35,7 @@ import OrbitDBAddress, { isValidAddress } from './address.js'
|
||||
import Manifests from './manifest.js'
|
||||
import { createId } from './utils/index.js'
|
||||
import pathJoin from './utils/path-join.js'
|
||||
import * as AccessControllers from './access-controllers/index.js'
|
||||
import { getAccessController } from './access-controllers/index.js'
|
||||
import IPFSAccessController from './access-controllers/ipfs.js'
|
||||
|
||||
/**
|
||||
@@ -168,7 +168,7 @@ const OrbitDB = async ({ ipfs, id, identity, keystore, directory } = {}) => {
|
||||
manifest = await manifests.get(addr.path)
|
||||
const acType = manifest.accessController.split('/', 2).pop()
|
||||
const acAddress = manifest.accessController.replaceAll(`/${acType}/`, '')
|
||||
AccessController = AccessControllers.get(acType)()
|
||||
AccessController = getAccessController(acType)()
|
||||
accessController = await AccessController({ orbitdb: { open, identity, ipfs }, identities, address: acAddress })
|
||||
name = manifest.name
|
||||
type = type || manifest.type
|
||||
@@ -234,4 +234,4 @@ const OrbitDB = async ({ ipfs, id, identity, keystore, directory } = {}) => {
|
||||
}
|
||||
}
|
||||
|
||||
export { OrbitDB as default, OrbitDBAddress, addDatabaseType, databaseTypes, AccessControllers }
|
||||
export { OrbitDB as default, OrbitDBAddress, addDatabaseType, databaseTypes }
|
||||
|
||||
Reference in New Issue
Block a user