-
Notifications
You must be signed in to change notification settings - Fork 9
[add] Signature model, page & back-end API #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { observable } from 'mobx'; | ||
| import { BaseModel, persist, restore, toggle } from 'mobx-restful'; | ||
|
|
||
| import { isServer } from './configuration'; | ||
|
|
||
| export const buffer2hex = (buffer: ArrayBufferLike) => | ||
| Array.from(new Uint8Array(buffer), x => x.toString(16).padStart(2, '0')).join(''); | ||
|
|
||
| export class SignatureModel extends BaseModel { | ||
| algorithm = { name: 'ECDSA', namedCurve: 'P-384', hash: { name: 'SHA-256' } }; | ||
|
|
||
| @persist() | ||
| @observable | ||
| accessor privateKey: CryptoKey | undefined; | ||
|
|
||
| @persist() | ||
| @observable | ||
| accessor publicKey = ''; | ||
|
|
||
| @persist() | ||
| @observable | ||
| accessor signatureMap = {} as Record<string, string>; | ||
|
|
||
| restored = !isServer() && restore(this, 'Signature'); | ||
|
|
||
| @toggle('uploading') | ||
| async makeKeyPair() { | ||
| await this.restored; | ||
|
|
||
| if (this.publicKey) return this.publicKey; | ||
|
|
||
| const { publicKey, privateKey } = await crypto.subtle.generateKey(this.algorithm, true, [ | ||
| 'sign', | ||
| 'verify', | ||
| ]); | ||
| this.privateKey = privateKey; | ||
|
|
||
| const JWK = await crypto.subtle.exportKey('jwk', publicKey); | ||
|
|
||
| return (this.publicKey = btoa(JSON.stringify(JWK))); | ||
| } | ||
|
|
||
| @toggle('uploading') | ||
| async sign(value: string) { | ||
| await this.restored; | ||
|
|
||
| let signature = this.signatureMap[value]; | ||
|
|
||
| if (signature) return signature; | ||
|
|
||
| if (!this.publicKey) await this.makeKeyPair(); | ||
|
|
||
| const rawSignature = await crypto.subtle.sign( | ||
| this.algorithm, | ||
| this.privateKey!, | ||
| new TextEncoder().encode(value), | ||
| ); | ||
| signature = buffer2hex(rawSignature); | ||
|
|
||
| this.signatureMap = { ...this.signatureMap, [value]: signature }; | ||
|
|
||
| return signature; | ||
| } | ||
|
TechQuery marked this conversation as resolved.
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { createKoaRouter, withKoaRouter } from 'next-ssr-middleware'; | ||
|
|
||
| import { safeAPI } from '../core'; | ||
|
|
||
| export const config = { api: { bodyParser: false } }; | ||
|
|
||
| const router = createKoaRouter(import.meta.url); | ||
|
|
||
| router.post('/verification', safeAPI, async context => { | ||
| const { algorithm, publicKey, value, signature } = Reflect.get(context.request, 'body'); | ||
|
|
||
| const rawAlgorithm = JSON.parse(atob(algorithm)), | ||
| rawPublicKey = JSON.parse(atob(publicKey)), | ||
| rawSignature = Buffer.from(signature, 'hex'), | ||
| encodedValue = new TextEncoder().encode(value); | ||
|
|
||
| const key = await crypto.subtle.importKey('jwk', rawPublicKey, rawAlgorithm, true, ['verify']); | ||
| const verified = await crypto.subtle.verify(rawAlgorithm, key, rawSignature, encodedValue); | ||
|
|
||
| context.status = verified ? 200 : 400; | ||
|
TechQuery marked this conversation as resolved.
|
||
| context.body = {}; | ||
| }); | ||
|
|
||
| export default withKoaRouter(router); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { computed, observable } from 'mobx'; | ||
| import { textJoin } from 'mobx-i18n'; | ||
| import { observer } from 'mobx-react'; | ||
| import { ObservedComponent } from 'mobx-react-helper'; | ||
| import { compose, RouteProps, router } from 'next-ssr-middleware'; | ||
| import { Container } from 'react-bootstrap'; | ||
| import { buildURLData } from 'web-utility'; | ||
|
|
||
| import { PageHead } from '../components/Layout/PageHead'; | ||
| import { i18n, I18nContext } from '../models/Translation'; | ||
| import { SignatureModel } from '../models/Signature'; | ||
|
|
||
| export const getServerSideProps = compose(router); | ||
|
|
||
| @observer | ||
| export default class SignaturePage extends ObservedComponent<RouteProps, typeof i18n> { | ||
| static contextType = I18nContext; | ||
|
|
||
| @observable | ||
| accessor signatureStore = new SignatureModel(); | ||
|
|
||
| @computed | ||
| get linkData() { | ||
| const { route } = this.observedProps; | ||
| const { valueName, algorithmName, publicKeyName, signatureName, value } = route.query, | ||
| { algorithm, publicKey } = this.signatureStore; | ||
| const signature = this.signatureStore.signatureMap[value + '']; | ||
|
|
||
| return buildURLData({ | ||
| [valueName + '']: value, | ||
| [algorithmName + '']: btoa(JSON.stringify(algorithm)), | ||
| [publicKeyName + '']: publicKey, | ||
| [signatureName + '']: signature, | ||
| }); | ||
| } | ||
|
Comment on lines
+23
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 当查询参数缺失时,
🤖 Prompt for AI Agents |
||
|
|
||
| componentDidMount() { | ||
| const { value = '' } = this.props.route.query; | ||
|
|
||
| if (!value) this.signatureStore.makeKeyPair(); | ||
| else this.signatureStore.sign(value + ''); | ||
| } | ||
|
TechQuery marked this conversation as resolved.
|
||
|
|
||
| render() { | ||
| const { t } = this.observedContext, | ||
| { value, iframeLink } = this.props.route.query; | ||
|
|
||
| const title = value ? textJoin(t('sign'), value + '') : t('generate_key_pair'), | ||
| link = `${iframeLink}?${this.linkData}`; | ||
|
|
||
| return ( | ||
| <Container> | ||
| <PageHead title={title} /> | ||
|
|
||
| <h1 className="my-5 text-truncate">{title}</h1> | ||
|
|
||
| <section className="markdown-body bg-white py-4"> | ||
| <blockquote>{t('signature_disclaimer')}</blockquote> | ||
| <pre> | ||
| <code> | ||
| <a href={link} target="_blank" rel="noopener noreferrer"> | ||
| {link} | ||
| </a> | ||
| </code> | ||
| </pre> | ||
| </section> | ||
|
|
||
| <iframe | ||
| className="border-0 w-100 vh-100" | ||
| sandbox="allow-scripts allow-same-origin allow-forms" | ||
| src={link} | ||
| /> | ||
|
Comment on lines
+44
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 当 如果用户直接访问
应在 🐛 建议修复:条件渲染 iframe 区域 render() {
const { t } = this.observedContext,
{ value, iframeLink } = this.props.route.query;
const title = value ? textJoin(t('sign'), value + '') : t('generate_key_pair'),
- link = `${iframeLink}?${this.linkData}`;
+ link = iframeLink ? `${iframeLink}?${this.linkData}` : '';
return (
<Container>
<PageHead title={title} />
<h1 className="my-5 text-truncate">{title}</h1>
<section className="markdown-body bg-white py-4">
<blockquote>{t('signature_disclaimer')}</blockquote>
- <pre>
- <code>
- <a href={link} target="_blank" rel="noopener noreferrer">
- {link}
- </a>
- </code>
- </pre>
+ {link && (
+ <pre>
+ <code>
+ <a href={link} target="_blank" rel="noopener noreferrer">
+ {link}
+ </a>
+ </code>
+ </pre>
+ )}
</section>
- <iframe
- className="border-0 w-100 vh-100"
- sandbox="allow-scripts allow-same-origin allow-forms"
- src={link}
- />
+ {link && (
+ <iframe
+ className="border-0 w-100 vh-100"
+ sandbox="allow-scripts allow-same-origin allow-forms"
+ src={link}
+ />
+ )}
</Container>
);
}🤖 Prompt for AI Agents |
||
| </Container> | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.