generated from idea2app/Next-Bootstrap-ts
-
Notifications
You must be signed in to change notification settings - Fork 6
[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
Merged
+1,390
−1,232
Merged
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,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.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or 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 hidden or 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,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.
Show resolved
Hide resolved
|
||
| context.body = {}; | ||
| }); | ||
|
|
||
| export default withKoaRouter(router); | ||
This file contains hidden or 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,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, | ||
| }); | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| const { value = '' } = this.props.route.query; | ||
|
|
||
| if (!value) this.signatureStore.makeKeyPair(); | ||
| else this.signatureStore.sign(value + ''); | ||
| } | ||
TechQuery marked this conversation as resolved.
Show resolved
Hide 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> | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.