-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
instead of using a single global api key for apps, anonymous sessions are used for getting a temporary access
- Loading branch information
1 parent
7c6e65e
commit a8f1bfd
Showing
5 changed files
with
91 additions
and
14 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,68 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
import {Account, Client, Databases, Functions, Models, AppwriteException} from 'appwrite' | ||
import config from '@/config/app-config' | ||
|
||
/** | ||
* Returns a Promise including an object with a 'tyk_api_key' property | ||
* @returns {Promise<Models.Document|any|undefined>} | ||
*/ | ||
export const getKeyObject = async () => { | ||
const client = new Client() | ||
.setEndpoint(config.appwrite.endpoint) // move to config | ||
.setProject(config.appwrite.projectId) // move to config | ||
|
||
const account = new Account(client) | ||
try { | ||
return await getExistingUserSession(account, client) | ||
} catch (error) { | ||
if (error.code === 401 && error.type === 'general_unauthorized_scope') { | ||
console.log('No existing Account') | ||
return await creatNewAnonUser(account, client) | ||
} else { | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns an existing anonymous user session | ||
* @param account | ||
* @param client | ||
* @returns {Promise<Models.Document>} | ||
* @throws AppwriteException | ||
*/ | ||
const getExistingUserSession = async (account, client) => { | ||
const currentUser = await account.get() | ||
const databases = new Databases(client) | ||
return await databases.getDocument( | ||
'tyk_integration', | ||
'tyk_anonymous_keys', | ||
currentUser.$id | ||
) | ||
} | ||
|
||
/** | ||
* Creates a new anonymous user & session. | ||
* Passes domain & configured policy to specific appwrite function | ||
* @param account | ||
* @param client | ||
* @returns {Promise<any>} | ||
*/ | ||
const creatNewAnonUser = async (account, client) => { | ||
try { | ||
await account.createAnonymousSession() | ||
const functions = new Functions(client) | ||
|
||
const res = await functions.createExecution( | ||
config.appwrite.executionId, | ||
JSON.stringify({ | ||
tag: window.location.origin, | ||
policy: config.appwrite.policy | ||
}), | ||
) | ||
return JSON.parse(res.responseBody) | ||
} catch (error) { | ||
console.error('Error creating anonymous user: ', error) | ||
throw error | ||
} | ||
} |