Skip to content

Commit

Permalink
fix: Merge branch 'main' of https://github.com/affinidi/reference-app…
Browse files Browse the repository at this point in the history
…-affinidi-vault into paramesh
  • Loading branch information
kamarthiparamesh committed Nov 23, 2023
2 parents 8c722a8 + 66ae696 commit db1812e
Show file tree
Hide file tree
Showing 67 changed files with 3,128 additions and 330 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ This repository contains multiple sample applications to get you started on inte

Uses [Django](https://www.djangoproject.com/) and [Authlib](https://docs.authlib.org/en/latest/client/django.html#django-client) with a custom Affinidi provider.

- [affinidi-flask-authlib](./samples/affinidi-flask-authlib)

Uses [Flask](https://flask.palletsprojects.com/) and [Authlib](https://docs.authlib.org/en/latest/client/flask.html) with a custom Affinidi provider.

- [affinidi-fastapi-authlib](./samples/affinidi-fastapi-authlib)

Uses [FastApi](https://fastapi.tiangolo.com/) and [Authlib](https://docs.authlib.org/en/latest/client/fastapi.html) with a custom Affinidi provider.

- [affinidi-starlette-authlib](./samples/affinidi-starlette-authlib)

Uses [starlette](https://www.starlette.io/) and [Authlib](https://docs.authlib.org/en/latest/client/starlette.html) with a custom Affinidi provider.

- [affinidi-laravel-socialite](./samples/affinidi-laravel-socialite)

Uses [PHP Laravel](https://laravel.com/) and [Socialite](https://laravel.com/docs/10.x/socialite) with a custom Affinidi provider.
Expand Down
21 changes: 0 additions & 21 deletions generator/django/generate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ async function generate() {

for (const [i, sample] of overrides.entries()) {
console.log(`\nGenerating "${sample}" sample`)
const port = 3000 + i + 1

const overridePath = join(overridesPath, sample)
const samplePath = join(samplesPath, sample)
Expand All @@ -46,8 +45,6 @@ async function generate() {
}
}

const { readmeReplacements } = generatorConfig

console.log('Copying the template')
const pathsToDelete = (await fs.readdir(samplePath).catch(() => []))
.filter((file) => !filesToIgnore.includes(file))
Expand Down Expand Up @@ -76,24 +73,6 @@ async function generate() {
}
}

async function transformJson(path, transformFn) {
const json = JSON.parse(await fs.readFile(path, { encoding: 'utf-8' }))
transformFn(json)
await fs.writeFile(path, JSON.stringify(json, null, 2), {
encoding: 'utf-8',
})
}

async function replace(path, replacements) {
let text = await fs.readFile(path, { encoding: 'utf-8' })

for (const [key, value] of Object.entries(replacements)) {
text = text.replaceAll(key, Array.isArray(value) ? value.join('\n') : value)
}

await fs.writeFile(path, text, { encoding: 'utf-8' })
}

async function merge(from, to, options) {
await mkdirp(join(to, '..'))

Expand Down
96 changes: 45 additions & 51 deletions generator/django/template/docs/setup-login-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ To update first create JSON configuration file with such content:

```json
{
"presentationDefinition": {
"presentationDefinition": {
"id": "vp_combined_email_user_profile_combined",
"submission_requirements": [
{
Expand All @@ -85,90 +85,84 @@ To update first create JSON configuration file with such content:
{
"id": "email_vc",
"name": "Email VC",
"purpose": "Check if VC type is correct",
"purpose": "Check if data contains necessary fields",
"group": ["A"],
"constraints": {
"fields": [
{
"path": ["$.credentialSchema.id"],
"path": [
"$.type"
],
"purpose": "Check if VC type is correct",
"filter": {
"type": "array",
"contains": {
"type": "string",
"pattern": "Email"
}
}
},
{
"path": [
"$.credentialSubject.email"
],
"purpose": "Check if VC contains email field",
"filter": {
"type": "string"
}
},
{
"path": [
"$.issuer"
],
"purpose": "Check if VC Issuer is Trusted",
"filter": {
"type": "string",
"pattern": "^https://schema.affinidi.com/EmailV1-0.json$"
"pattern": "^did:key:zQ3shtMGCU89kb2RMknNZcYGUcHW8P6Cq3CoQyvoDs7Qqh33N"
}
}
]
}
},
{
"id": "profile_vc",
"name": "profile VC type",
"purpose": "Check if VC type is correct",
"name": "Country VC",
"purpose": "Check if data contains necessary fields",
"group": ["A"],
"constraints": {
"fields": [
{
"path": ["$.credentialSchema.id"],
"path": ["$.type"],
"purpose": "Check if VC type is correct",
"filter": {
"type": "string",
"pattern": "^https://schema.affinidi.com/UserProfileV2-0.json$"
"type": "array",
"pattern": "UserProfile"
}
}
]
}
},
{
"id": "address",
"name": "Address",
"purpose": "To get address for ID Mapping",
"constraints": {
"fields": [
{
"path": ["$.credentialSubject.address"]
}
]
}
},
{
"id": "email",
"name": "Email",
"purpose": "To get email for ID Mapping",
"constraints": {
"fields": [
{
"path": ["$.credentialSubject.email"]
}
]
}
},
{
"id": "type",
"name": "Type",
"purpose": "To get type for ID Mapping",
"constraints": {
"fields": [
},
{
"path": ["$.type"]
"path": ["$.credentialSubject.address.country"],
"purpose": "Check if VC contains address field",
"filter": {
"type": "string"
}
}
]
}
}
]
},
},
"idTokenMapping": [
{
"sourceField": "$.type",
"idTokenClaim": "type"
},
{
"sourceField": "$.credentialSubject.email",
"idTokenClaim": "email"
},
{
"sourceField": "$.credentialSubject.address",
"idTokenClaim": "address"
"sourceField": "$.credentialSubject.address.country",
"idTokenClaim": "country"
}
]
}

```

After saving file run command:
Expand Down
13 changes: 13 additions & 0 deletions generator/fastapi/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"overrides": [
{
"files": ["generate.mjs", "confirm.mjs"],
"options": {
"printWidth": 120,
"semi": false,
"singleQuote": true,
"trailingComma": "all"
}
}
]
}
16 changes: 16 additions & 0 deletions generator/fastapi/confirm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import inquirer from 'inquirer'

const { confirmed } = await inquirer.prompt([
{
default: true,
name: 'confirmed',
message: 'This will overwrite any changes made to the samples directory. Are you sure?',
type: 'confirm',
},
])

if (confirmed) {
process.exit(0)
} else {
process.exit(1)
}
111 changes: 111 additions & 0 deletions generator/fastapi/generate.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import fs from 'fs/promises'
import { dirname, join, basename } from 'path'
import url from 'url'
import mkdirp from 'mkdirp'
import rimraf from 'rimraf'
import { generateAppInformation } from '../generateAppInformation.mjs'

const filesToIgnore = ['.next', '.env', 'generator-config.json', '.gitkeep', 'keys', 'appInformation.json']
const pathsToOverwrite = []

const __dirname = dirname(url.fileURLToPath(import.meta.url))

async function generate() {
await generateAppInformation()
const rootPath = join(__dirname, '../..')
const samplesPath = join(rootPath, 'samples')
const generatorPath = join(rootPath, 'generator')
const fastapiPath = join(generatorPath, 'fastapi')
const overridesPath = join(fastapiPath, 'overrides')
const templatePath = join(fastapiPath, 'template')

const overrides = (await fs.readdir(overridesPath, { withFileTypes: true }))
.filter((i) => i.isDirectory())
.map((i) => i.name)
.sort()

console.log(`Detected samples: ${overrides.join(', ')}`)

for (const [i, sample] of overrides.entries()) {
console.log(`\nGenerating "${sample}" sample`)

const overridePath = join(overridesPath, sample)
const samplePath = join(samplesPath, sample)

let generatorConfig = {}
try {
generatorConfig = JSON.parse(
await fs.readFile(join(overridePath, 'generator-config.json'), {
encoding: 'utf-8',
}),
)
} catch (error) {
if (error.code !== 'ENOENT') {
throw error
}
}

console.log('Copying the template')
const pathsToDelete = (await fs.readdir(samplePath).catch(() => []))
.filter((file) => !filesToIgnore.includes(file))
.map((file) => join(samplePath, file))
await deletePath(pathsToDelete)
await merge(templatePath, samplePath, {
filter: (path) => !filesToIgnore.includes(basename(path)),
})

for (const path of pathsToOverwrite) {
if (await exists(join(overridePath, ...path))) {
console.log(`Deleting "${path.join('/')}" path from the template`)
await deletePath(join(samplePath, ...path))
}
}

console.log(`Applying overrides`)
await merge(overridePath, samplePath, {
filter: (path) => !filesToIgnore.includes(basename(path)),
})

const envPath = join(samplePath, '.env')
if (!(await exists(envPath))) {
await fs.cp(join(samplePath, '.env.example'), envPath)
}
}
}

async function merge(from, to, options) {
await mkdirp(join(to, '..'))

try {
await fs.cp(from, to, { recursive: true, ...options })
} catch (error) {
if (error.code === 'ENOENT') {
console.warn(`Warning: Source doesn't exist: ${from}`)
} else {
throw error
}
}
}

async function deletePath(path) {
await rimraf(path)
}

async function exists(path) {
try {
await fs.access(path)
return true
} catch {
return false
}
}

generate()
.catch((error) => {
console.error(error)
process.exit(1)
})
.then(() => {
console.log('\nDone!')
process.exit(0)
})
Empty file.
4 changes: 4 additions & 0 deletions generator/fastapi/template/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PROVIDER_CLIENT_ID=""
PROVIDER_CLIENT_SECRET=""
PROVIDER_ISSUER=""
FASTAPI_SECRET="this-is-very-secret-dont-tell-anyone"
Loading

0 comments on commit db1812e

Please sign in to comment.