better implementation

This commit is contained in:
Hailey
2024-04-14 23:27:27 -07:00
parent 71f1e44653
commit 2ada0cbd0d
12 changed files with 136 additions and 62 deletions
@@ -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)
}
}