Skip to content
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

Create @magic-sdk/react-native-bare SDK #401

Merged
Merged
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ jobs:
- store_artifacts:
path: packages/@magic-sdk/provider/coverage
- store_artifacts:
path: packages/@magic-sdk/react-native/coverage
path: packages/@magic-sdk/react-native-bare/coverage
- store_artifacts:
path: packages/magic-sdk/coverage

Expand Down
2 changes: 1 addition & 1 deletion BUILD_SYSTEM.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ We generate multiple outputs in order to maximize backwards compatibility and in
// One of: node | browser | neutral
//
// node: Targets Node-like runtimes; used for React Native & server-side
// packages (i.e.: `@magic-sdk/react-native`)
// packages (i.e.: `@magic-sdk/react-native-bare`)
//
// browser: Targets web-like runtimes; used for web-only packages
// (i.e.: `magic-sdk`)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
## 📖 Documentation

- See the [developer documentation](https://docs.magic.link) to learn how you can master the Magic SDK in a matter of minutes.
- See the `@magic-sdk/react-native` [README](https://github.com/magiclabs/magic-js/tree/master/packages/%40magic-sdk/react-native#readme) for package specific information.
- See the `@magic-sdk/react-native-bare` [README](https://github.com/magiclabs/magic-js/tree/master/packages/%40magic-sdk/react-native-bare#readme) for Bare React Native package specific information.

## ⚡️ Quick Start

Expand All @@ -24,11 +24,11 @@ Integrating your app with Magic will require our client-side NPM package:
```bash
# Via NPM:
npm install --save magic-sdk # If you're targeting web browsers
npm install --save @magic-sdk/react-native # If you're targeting React Native
npm install --save @magic-sdk/react-native-bare # If you're targeting Bare React Native

# Via Yarn:
yarn add magic-sdk # If you're targeting web browsers
yarn add @magic-sdk/react-native # If you're targeting React Native
yarn add @magic-sdk/react-native-bare # If you're targeting Bare React Native
```

Alternatively, you can load via CDN with by adding a script tag to your app’s `<head>`:
Expand Down Expand Up @@ -60,7 +60,7 @@ These are packages _you_ can install to enable Magic JS SDK functionality for yo
| Package Name | Changelog | Description |
| ------------ | --------- | ----------- |
| [`magic-sdk`](https://www.npmjs.com/package/magic-sdk) | [CHANGELOG](./packages/magic-sdk/CHANGELOG.md) | Web/browser entry-point for Magic SDK. |
| [`@magic-sdk/react-native`](https://www.npmjs.com/package/@magic-sdk/react-native) | [CHANGELOG](./packages/@magic-sdk/react-native/CHANGELOG.md) | React Native entry-point for Magic SDK. |
| [`@magic-sdk/react-native-bare`](https://www.npmjs.com/package/@magic-sdk/react-native-bare) | [CHANGELOG](./packages/@magic-sdk/react-native-bare/CHANGELOG.md) | Bare React Native entry-point for Magic SDK. |

## Extensions

Expand Down Expand Up @@ -88,7 +88,7 @@ yarn test
Test an individual package
```bash
PKG=magic-sdk yarn test
PKG=@magic-sdk/react-native yarn test
PKG=@magic-sdk/react-native-bare yarn test
```

Test specific files
Expand Down
5 changes: 5 additions & 0 deletions packages/@magic-ext/bare-react-native-oauth/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
/coverage
/dist
/.eslintrc.js
/jest.config.ts
7 changes: 7 additions & 0 deletions packages/@magic-ext/bare-react-native-oauth/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
extends: ['../../../.eslintrc.js'],
parserOptions: {
project: ['./tsconfig.json'],
tsconfigRootDir: __dirname,
},
}
110 changes: 110 additions & 0 deletions packages/@magic-ext/bare-react-native-oauth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# v5.0.0 (Tue Dec 20 2022)

#### 🐛 Bug Fix

- Merge branch 'master' into mushfichowdhury-sc-61149-implement-event-emitting-in-magic-sdk-based ([@mushfichowdhury-magic](https://github.com/mushfichowdhury-magic))
- Adds more Context about Expo dependency in `@magic-ext/react-native-oauth` to README [#386](https://github.com/magiclabs/magic-js/pull/386) ([@Ariflo](https://github.com/Ariflo))

#### ⚠️ Pushed to `master`

- Add bundleId to the OAuth query ([@Ethella](https://github.com/Ethella))

#### Authors: 3

- Arian Flores ([@Ariflo](https://github.com/Ariflo))
- Jerry Liu ([@Ethella](https://github.com/Ethella))
- Mushfi Chowdhury ([@mushfichowdhury-magic](https://github.com/mushfichowdhury-magic))

---

# v3.1.0 (Wed Aug 10 2022)

#### 🚀 Enhancement

- bump web-expo-browser version [#345](https://github.com/magiclabs/magic-js/pull/345) ([@Ethella](https://github.com/Ethella))

#### Authors: 1

- Jerry Liu ([@Ethella](https://github.com/Ethella))

---

# v2.2.0 (Mon Mar 07 2022)

#### 🐛 Bug Fix

- Merge branch 'master' into smithki/better-build-system-documentation ([@smithki](https://github.com/smithki))

#### Authors: 1

- Ian K Smith ([@smithki](https://github.com/smithki))

---

# v2.0.0 (Tue Jan 25 2022)

#### 💥 Breaking Change

- Switch from `microbundle` to `esbuild` [#220](https://github.com/magiclabs/magic-js/pull/220) ([@smithki](https://github.com/smithki))

#### 🐛 Bug Fix

- Merge with master ([@smithki](https://github.com/smithki))
- Merge branch 'master' into feat/faster-builds ([@smithki](https://github.com/smithki))
- Fix build errors related to isolatedModules ([@smithki](https://github.com/smithki))

#### Authors: 1

- Ian K Smith ([@smithki](https://github.com/smithki))

---

# v0.5.4 (Thu Oct 21 2021)

#### 🐛 Bug Fix

- Revert to `.js` extension for ES module builds targeting webpack/CRA [#232](https://github.com/magiclabs/magic-js/pull/232) ([@smithki](https://github.com/smithki))

#### Authors: 1

- Ian K Smith ([@smithki](https://github.com/smithki))

---

# v0.5.3 (Wed Oct 20 2021)

#### ⚠️ Pushed to `master`

- Merge branch 'master' of github.com:magiclabs/magic-js ([@smithki](https://github.com/smithki))
- Force re-publish ([@smithki](https://github.com/smithki))

#### Authors: 1

- Ian K Smith ([@smithki](https://github.com/smithki))

---

# v0.5.2 (Wed Oct 20 2021)

#### 🐛 Bug Fix

- Use '.mjs' extension for ESM build files [#230](https://github.com/magiclabs/magic-js/pull/230) ([@smithki](https://github.com/smithki))

#### Authors: 1

- Ian K Smith ([@smithki](https://github.com/smithki))

---

# v0.4.1 (Thu Sep 23 2021)

#### 🐛 Bug Fix

- Port `@magic-ext/oauth`, `@magic-ext/react-native-oauth`, `@magic-ext/webauthn` extensions to Magic SDK monorepo [#218](https://github.com/magiclabs/magic-js/pull/218) ([@smithki](https://github.com/smithki))

#### Authors: 1

- Ian K Smith ([@smithki](https://github.com/smithki))

---

21 changes: 21 additions & 0 deletions packages/@magic-ext/bare-react-native-oauth/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-present Magic Labs, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions packages/@magic-ext/bare-react-native-oauth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@





37 changes: 37 additions & 0 deletions packages/@magic-ext/bare-react-native-oauth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@magic-ext/bare-react-native-oauth",
"version": "6.0.0",
"description": "Magic SDK OAuth Extension for React Native environments.",
"author": "Magic <[email protected]> (https://magic.link/)",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/magiclabs/magic-js"
},
"files": [
"dist"
],
"target": "node",
"main": "./dist/cjs/index.js",
"module": "./dist/es/index.js",
"types": "./dist/types/index.d.ts",
"exports": {
"import": "./dist/es/index.mjs",
"require": "./dist/cjs/index.js"
},
"externals": {
"include": [
"@magic-sdk/react-native-bare"
]
},
"dependencies": {
"@magic-sdk/types": "^10.0.0",
"crypto-js": "^3.3.0",
"react-native-device-info": "^10.3.0",
"react-native-inappbrowser-reborn": "^3.7.0"
},
"devDependencies": {
"@magic-sdk/react-native-bare": "^12.0.0",
"@types/crypto-js": "~3.1.47"
}
}
81 changes: 81 additions & 0 deletions packages/@magic-ext/bare-react-native-oauth/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { WordArray } from 'crypto-js';
import sha256Fallback from 'crypto-js/sha256';
import Base64 from 'crypto-js/enc-base64';

const CHARSET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~';
const HAS_CRYPTO = typeof window !== 'undefined' && !!(window.crypto as any);
const HAS_SUBTLE_CRYPTO = HAS_CRYPTO && !!(window.crypto.subtle as any);

/**
* Stringifies `bytes` using the OAuth 2.0 `code_verifier` character set.
*/
function bytesToVerifierString(bytes: Uint8Array) {
return Array.from(bytes)
.map((value: number) => CHARSET[value % CHARSET.length])
.join('');
}

/**
* Stringifies argument (as CryptoJS `WordArray` or EcmaScript `ArrayBuffer`)
* and encodes to URL-safe Base64.
*/
function base64URLEncodeFromByteArray(wordArray: WordArray): string;
function base64URLEncodeFromByteArray(arrayBuffer: ArrayBuffer): string;
function base64URLEncodeFromByteArray(arg: WordArray | ArrayBuffer): string {
const makeURLSafe = (base64: string) => {
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
};

if (arg instanceof ArrayBuffer) {
const bytes = new Uint8Array(arg);
const utf8Binary = Array.from(bytes)
.map((value) => String.fromCharCode(value))
.join('');

const base64 = btoa(utf8Binary);
return makeURLSafe(base64);
}

return makeURLSafe(Base64.stringify(arg));
}

/**
* Produces a SHA-256 hash of the given `message`. This function first attempts
* to use the browser's built-in `SubtleCrypto` API, falling back to
* CryptoJS if required.
*/
async function sha256(message: string) {
if (HAS_SUBTLE_CRYPTO) {
const bytes = new TextEncoder().encode(message);
return crypto.subtle.digest('SHA-256', bytes).then(base64URLEncodeFromByteArray);
}

return base64URLEncodeFromByteArray(sha256Fallback(message));
}

/**
* Creates a cryptographically random string using the browser's built-in
* `Crypto` API, falling back to `Math.random` if required.
*/
function createRandomString(size: number) {
const bytes = new Uint8Array(size);

if (HAS_CRYPTO) {
window.crypto.getRandomValues(bytes);
} else {
for (let i = 0; i < size; i += 1) bytes[i] = Math.floor(Math.random() * Math.floor(255));
}

return bytesToVerifierString(bytes);
}

/**
* Creates OAuth 2.0-compatible `code_verifier`, `code_challenge`, and `state`
* parameters.
*/
export async function createCryptoChallenge() {
const state = createRandomString(128);
const verifier = createRandomString(128);
const challenge = await sha256(verifier);
return { verifier, challenge, state };
}
Loading