This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
feat: add firebase-access-controller #44
Open
vasa-develop
wants to merge
1
commit into
orbitdb-archive:main
Choose a base branch
from
vasa-develop:feat/firebase
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"use strict"; | ||
|
||
const EventEmitter = require("events").EventEmitter; | ||
const io = require("orbit-db-io"); | ||
/** | ||
* A Firebase based AccessController for AvionDB & OrbitDB | ||
* | ||
* A demo app using FirebaseAccessController | ||
* https://github.com/dappkit/aviondb-firebase | ||
* | ||
*/ | ||
class FirebaseAccessController extends EventEmitter { | ||
constructor(ipfs, options) { | ||
super(); | ||
this._ipfs = ipfs; | ||
this.firebase = options.firebase; | ||
this.firebaseConfig = options.firebaseConfig; | ||
if (this.firebase.apps.length === 0) { | ||
this.firebase.initializeApp(this.firebaseConfig); | ||
} | ||
} | ||
|
||
/* | ||
Every AC needs to have a 'Factory' method | ||
that creates an instance of the AccessController | ||
*/ | ||
static async create(orbitdb, options) { | ||
console.log(options); | ||
if (!options.firebaseConfig) { | ||
throw new Error("you need to pass a firebaseConfig Object"); | ||
} | ||
return new FirebaseAccessController(orbitdb._ipfs, options); | ||
} | ||
|
||
/* Return the type for this controller */ | ||
static get type() { | ||
return "firebase-access-controller"; | ||
} | ||
|
||
/* | ||
Return the type for this controller | ||
*/ | ||
get type() { | ||
return this.constructor.type; | ||
} | ||
|
||
/* | ||
Called by the databases (the log) to see if entry should | ||
be allowed in the database. Return true if the entry is allowed, | ||
false is not allowed | ||
*/ | ||
async canAppend(entry, identityProvider) { | ||
return new Promise((resolve, reject) => { | ||
this.firebase.auth().onAuthStateChanged(async (user) => { | ||
if (user) { | ||
// A user is signed in | ||
const verifiedIdentity = await identityProvider.verifyIdentity( | ||
entry.identity | ||
); | ||
// Allow access if identity verifies | ||
return resolve(verifiedIdentity); | ||
} else { | ||
// No user is signed in | ||
return resolve(false); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
/* Add access */ | ||
async grant(user) { | ||
await this.firebase.auth().createUser(user); | ||
} | ||
/* Remove access */ | ||
async revoke() { | ||
await this.firebase.auth().currentUser.delete(); | ||
} | ||
|
||
/* AC creation and loading */ | ||
async load(address) { | ||
if (address) { | ||
try { | ||
if (address.indexOf("/ipfs") === 0) { | ||
address = address.split("/")[2]; | ||
} | ||
const access = await io.read(this._ipfs, address); | ||
this.firebaseConfig = access.firebaseConfig; | ||
} catch (e) { | ||
console.log("FirebaseAccessController.load ERROR:", e); | ||
} | ||
} | ||
} | ||
/* Returns AC manifest parameters object */ | ||
async save() { | ||
let cid; | ||
try { | ||
cid = await io.write(this._ipfs, "dag-cbor", { | ||
firebaseConfig: this.firebaseConfig, | ||
}); | ||
} catch (e) { | ||
console.log("FirebaseAccessController.save ERROR:", e); | ||
} | ||
// return the manifest data | ||
return { address: cid }; | ||
} | ||
} | ||
|
||
export default FirebaseAccessController; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did
resolve(false)
instead ofreject(false)
as this would need the developer to catch the error whencanAppend
is rejected, which is not good in terms of developer experience.