better implementation
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import {requireNativeModule} from 'expo-modules-core'
|
||||
import {Jwk, Jwt} from '@atproto/jwk'
|
||||
import {Jwk, Jwt, Key} from '@atproto/jwk'
|
||||
|
||||
const NativeModule = requireNativeModule('ExpoBlueskyOAuthClient')
|
||||
|
||||
@@ -14,7 +14,7 @@ export const OauthClientReactNative = (NativeModule as null) || {
|
||||
/**
|
||||
* @throws if the algorithm is not supported ("sha256" must be supported)
|
||||
*/
|
||||
digest(_bytes: Uint8Array, _algorithm: string): Uint8Array {
|
||||
async digest(_bytes: Uint8Array, _algorithm: string): Promise<Uint8Array> {
|
||||
throw new Error(LINKING_ERROR)
|
||||
},
|
||||
|
||||
@@ -24,21 +24,25 @@ export const OauthClientReactNative = (NativeModule as null) || {
|
||||
*
|
||||
* @throws if the algorithm is not supported ("ES256" must be supported)
|
||||
*/
|
||||
generateJwk(_algo: string): Jwk {
|
||||
async generateJwk(_algo: string): Promise<{publicKey: Key; privateKey: Key}> {
|
||||
throw new Error(LINKING_ERROR)
|
||||
},
|
||||
|
||||
createJwt(_header: unknown, _payload: unknown, _jwk: unknown): Jwt {
|
||||
async createJwt(
|
||||
_header: unknown,
|
||||
_payload: unknown,
|
||||
_jwk: unknown,
|
||||
): Promise<Jwt> {
|
||||
throw new Error(LINKING_ERROR)
|
||||
},
|
||||
|
||||
verifyJwt(
|
||||
async verifyJwt(
|
||||
_token: Jwt,
|
||||
_jwk: Jwk,
|
||||
): {
|
||||
): Promise<{
|
||||
payload: Record<string, unknown>
|
||||
protectedHeader: Record<string, unknown>
|
||||
} {
|
||||
}> {
|
||||
throw new Error(LINKING_ERROR)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import {CryptoImplementaton, DigestAlgorithm, Key} from '@atproto/oauth-client'
|
||||
import {CryptoImplementation, DigestAlgorithm, Key} from '@atproto/oauth-client'
|
||||
|
||||
import {OauthClientReactNative} from './oauth-client-react-native'
|
||||
import {ReactNativeKey} from './react-native-key'
|
||||
|
||||
export class ReactNativeCryptoImplementation implements CryptoImplementaton {
|
||||
export class ReactNativeCryptoImplementation implements CryptoImplementation {
|
||||
async createKey(algs: string[]): Promise<Key> {
|
||||
const bytes = await this.getRandomValues(12)
|
||||
const kid = Array.from(bytes, byteToHex).join('')
|
||||
return ReactNativeKey.generate(kid, algs)
|
||||
return await ReactNativeKey.generate(kid, algs)
|
||||
}
|
||||
|
||||
async getRandomValues(length: number): Promise<Uint8Array> {
|
||||
|
||||
@@ -19,7 +19,12 @@ export class ReactNativeKey extends Key {
|
||||
try {
|
||||
// Note: OauthClientReactNative.generatePrivateJwk should throw if it
|
||||
// doesn't support the algorithm.
|
||||
const jwk = await OauthClientReactNative.generateJwk(algo)
|
||||
const res = await OauthClientReactNative.generateJwk(algo)
|
||||
const jwk = jwkValidator.parse({
|
||||
...res.privateKey,
|
||||
key_ops: ['sign', 'verify'],
|
||||
kid,
|
||||
})
|
||||
const use = jwk.use || 'sig'
|
||||
return new ReactNativeKey(jwkValidator.parse({...jwk, use, kid}))
|
||||
} catch {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import {GenericStore, Value} from '@atproto/caching'
|
||||
import {Jwk} from '@atproto/jwk'
|
||||
|
||||
import {ReactNativeKey} from './react-native-key.js'
|
||||
import {ReactNativeStore} from './react-native-store.js'
|
||||
import {ReactNativeKey} from './react-native-key'
|
||||
import {ReactNativeStore} from './react-native-store'
|
||||
|
||||
type ExposedValue = Value & {dpopKey: ReactNativeKey}
|
||||
type StoredValue<V extends ExposedValue> = Omit<V, 'dpopKey'> & {
|
||||
|
||||
@@ -20,6 +20,6 @@ export class ReactNativeStore<V extends Value>
|
||||
}
|
||||
|
||||
async del(key: string): Promise<void> {
|
||||
await Storage.delete(key)
|
||||
await Storage.removeItem(key)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user