-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: creating api throught dashboard
- Loading branch information
Nicolas Burtey
committed
Oct 23, 2023
1 parent
ca3c8b0
commit 668dc02
Showing
23 changed files
with
403 additions
and
53 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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# REPLACE THIS IT IS FOR TESTING | ||
NEXTAUTH_URL=https://c890-2405-201-301c-5b67-89d6-bd56-6afb-6294.ngrok-free.app | ||
NEXTAUTH_URL=http://localhost:3001 | ||
NEXTAUTH_SECRET="thisismysecret" | ||
# 2db7666c39074da4b399e8b5116ef2c6 | ||
# 2cc1869e52ad47df848a6519b63bb4f4 |
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,19 @@ | ||
import { NextResponse } from "next/server" | ||
import { getOauth2Client } from "../client" | ||
import { env } from "@/env" | ||
|
||
export async function GET(request: Request) { | ||
const client = await getOauth2Client() | ||
|
||
const params = client.callbackParams(request.url) | ||
|
||
// const tokenSet = await client.oauthCallback( | ||
const tokenSet = await client.callback( | ||
`${env.NEXTAUTH_URL}/api-keys/callback`, | ||
params, | ||
{ state: params.state }, | ||
) | ||
console.log("received and validated tokens %j", tokenSet) | ||
|
||
return NextResponse.json({ ...tokenSet }) | ||
} |
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,16 @@ | ||
import { env } from "@/env" | ||
import { Issuer } from "openid-client" | ||
|
||
const clientId = env.CLIENT_ID_APP_API_KEY | ||
const clientSecret = env.CLIENT_SECRET_APP_API_KEY | ||
|
||
export const getOauth2Client = async () => { | ||
const GaloyIssuer = await Issuer.discover(env.HYDRA_PUBLIC) | ||
|
||
return new GaloyIssuer.Client({ | ||
client_id: clientId, | ||
client_secret: clientSecret, | ||
redirect_uris: [`${env.NEXTAUTH_URL}/api-keys/callback`], | ||
response_types: ["code"], | ||
}) | ||
} |
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,20 @@ | ||
import { redirect } from "next/navigation" | ||
import { getOauth2Client } from "./client" | ||
|
||
const crypto = require("crypto") | ||
|
||
function generateSecureRandomString(length: number) { | ||
return crypto.randomBytes(length).toString("hex").slice(0, length) | ||
} | ||
|
||
export async function GET(request: Request) { | ||
const client = await getOauth2Client() | ||
const randomString = generateSecureRandomString(16) | ||
|
||
const authorizationUri = client.authorizationUrl({ | ||
scope: "transactions:read payments:send openid", | ||
state: randomString, | ||
}) | ||
|
||
redirect(authorizationUri) | ||
} |
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,5 @@ | ||
"use server"; | ||
|
||
export const deleteKey = async (index: string) => { | ||
console.log(index, "session id") | ||
} |
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,105 @@ | ||
import { getServerSession } from "next-auth"; | ||
import { redirect } from "next/navigation"; | ||
import { authOptions } from "../api/auth/[...nextauth]/route"; | ||
import { env } from "@/env"; | ||
import ResultDisplay from "@/components/keys/result-display"; | ||
|
||
const addKey = async () => { | ||
"use server" | ||
|
||
redirect("/api-keys") | ||
} | ||
|
||
// FIXME: userId not necessary? | ||
const deleteKeys = async () => { | ||
"use server" | ||
|
||
const session = await getServerSession(authOptions) | ||
const userId = session?.sub | ||
|
||
const subject = userId | ||
const CLIENT_ID_APP_API_KEY = env.CLIENT_ID_APP_API_KEY; | ||
|
||
const baseUrl = env.HYDRA_ADMIN; | ||
const url = `${baseUrl}/admin/oauth2/auth/sessions/consent?subject=${subject}&client=${CLIENT_ID_APP_API_KEY}`; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: 'DELETE', | ||
}); | ||
|
||
if (!response.ok) { | ||
const errorData = await response.json(); | ||
throw new Error('Server-side error: ' + errorData.error); | ||
} | ||
|
||
const data = await response.json(); | ||
console.log('Success:', data); | ||
} catch (error) { | ||
if (error instanceof Error) console.error('Error:', error.message); | ||
} | ||
|
||
redirect("/keys") | ||
} | ||
|
||
type ConsentObject = { | ||
grant_scope: string[], | ||
grant_access_token_audience: any[], | ||
session: { | ||
access_token: object, | ||
id_token: object | ||
}, | ||
remember: boolean, | ||
remember_for: number, | ||
handled_at: string, | ||
consent_request: { | ||
challenge: string, | ||
requested_scope: string[], | ||
requested_access_token_audience: any[], | ||
skip: boolean, | ||
subject: string, | ||
oidc_context: object, | ||
client: { | ||
client_id: string, | ||
[key: string]: any // This is to cover other properties of the 'client' object. | ||
}, | ||
[key: string]: any // This is to cover other properties of the 'consent_request' object. | ||
}, | ||
[key: string]: any // This is to cover other properties of the main object. | ||
}; | ||
|
||
export default async function page() { | ||
const session = await getServerSession(authOptions) | ||
const userId = session?.sub | ||
|
||
let keys: ConsentObject[] = []; | ||
|
||
try { | ||
const url = `http://localhost:4445/admin/oauth2/auth/sessions/consent?subject=${userId}`; | ||
const response = await fetch(url); | ||
const result = await response.json() as ConsentObject[]; | ||
keys = result.filter(item => item.consent_request.client.client_id === env.CLIENT_ID_APP_API_KEY) | ||
|
||
console.dir(keys, { depth: null}) | ||
} catch (error) { | ||
console.error('Error fetching consent session:', error); | ||
} | ||
|
||
return ( | ||
<> | ||
<div className="grid-flow-col grid p-6"> | ||
<form action={addKey}> | ||
<button className="bg-red-500 text-white px-3 py-1 rounded hover:bg-red-600">Add key</button> | ||
</form> | ||
<form action={deleteKeys}> | ||
<button className="bg-red-500 text-white px-3 py-1 rounded hover:bg-red-600">Delete keys</button> | ||
</form> | ||
</div> | ||
|
||
{keys.length ? <div className="p-6"> | ||
<h2 className="text-2xl font-semibold mb-8">Existing keys</h2> | ||
<ResultDisplay data={keys} /> | ||
</div>: null} | ||
</> | ||
) | ||
} |
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
Oops, something went wrong.