Skip to content

Commit

Permalink
added PKCE helper library for swan authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMeer committed Jan 17, 2021
1 parent 5c3bf0e commit 4ce9006
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 139 deletions.
264 changes: 132 additions & 132 deletions ios/HEXA.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ios/Podfile.lock
Expand Up @@ -773,4 +773,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 579ac7919977593ab0196f9f26d3bc979b90364a

COCOAPODS: 1.9.3
COCOAPODS: 1.8.4
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -50,6 +50,7 @@
"assert": "^1.5.0",
"asyncstorage-down": "^4.2.0",
"axios": "^0.21.1",
"base64url": "^3.0.1",
"bip21": "^2.0.2",
"bip32": "^2.0.4",
"bip39": "^3.0.2",
Expand Down
3 changes: 3 additions & 0 deletions src/bitcoin/HexaConfig.ts
Expand Up @@ -16,6 +16,9 @@ import {
import PersonalNode from '../common/data/models/PersonalNode'
import _ from 'lodash'
class HexaConfig {
//SWAN details
public SWAN_CLIENT_ID:string = Config.SWAN_CLIENT_ID || 'demo-web-client'
public SWAN_BASE_URL:string = Config.SWAN_AUTH_URL || 'https://dev-api.swanbitcoin.com'
public TESTNET_BASE_URL: string = Config.BIT_TESTNET_BASE_URL ? Config.BIT_TESTNET_BASE_URL.trim() : 'https://testapi.bithyve.com'
public MAINNET_BASE_URL: string = Config.BIT_MAINNET_BASE_URL ? Config.BIT_MAINNET_BASE_URL.trim() : 'https://api.bithyve.com'
public VERSION: string = Config.VERSION ? Config.VERSION.trim() : '';
Expand Down
20 changes: 17 additions & 3 deletions src/pages/SwanIntegration/SwanIntegrationScreen.tsx
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useEffect } from 'react'
import { Linking, View, Text, StyleSheet } from 'react-native'
import { useDispatch } from 'react-redux'
import ActionMenuListItem from './ActionMenuListItem'
Expand All @@ -8,6 +8,8 @@ import { fetchSwanAuthenticationUrl, linkSwanWallet, syncSwanWallet, SwanActionK
import { addNewAccountShell } from '../../store/actions/accounts'
import ExternalServiceSubAccountInfo from '../../common/data/models/SubAccountInfo/ExternalServiceSubAccountInfo'
import ServiceAccountKind from '../../common/data/enums/ServiceAccountKind'
import openLink from '../../utils/OpenLink'

export type Props = {
navigation: any;
};
Expand Down Expand Up @@ -45,7 +47,19 @@ const actionItemKeyExtractor = ( item: ActionMenuItem ) => String( item.kind )


const SwanIntegrationScreen: React.FC<Props> = ( { navigation, }: Props ) => {
const { hasFetchSwanAuthenticationUrlSucceeded, swanAuthenticationUrl } = useSwanIntegrationState()
const dispatch = useDispatch()
useEffect( ()=>{
dispatch( fetchSwanAuthenticationUrl( {
} ) )
}, [] )

useEffect( ()=>{
console.log( 'inside use effect', {
hasFetchSwanAuthenticationUrlSucceeded, swanAuthenticationUrl
} )
if( hasFetchSwanAuthenticationUrlSucceeded && swanAuthenticationUrl ) openLink( swanAuthenticationUrl )
}, [ hasFetchSwanAuthenticationUrlSucceeded, swanAuthenticationUrl ] )

function handleItemSelection( { kind: itemKind }: ActionMenuItem ) {
switch ( itemKind ) {
Expand All @@ -71,8 +85,8 @@ const SwanIntegrationScreen: React.FC<Props> = ( { navigation, }: Props ) => {
// ) )
break
case SwanActionKind.SYNC_SWAN_ACCOUNT_DATA:
Linking.openURL( 'https://dev-api.swanbitcoin.com?client_id=demo-web-client&state=1599045135410-jFe&scope=openid%20profile%20read&response_type=code&code_challenge=SfO9AIeVOoLBdi8xF5VF5ByzExMx4bxGDRsXUYMVRWc&code_challenge_method=S256&prompt=login&ui_locales=en&nonce=1599046102647-dv4&redirect_uri=https://oauth.tools/callback/code' )
// dispatch(syncSwanWallet({}));
dispatch( fetchSwanAuthenticationUrl( {
} ) )
break
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/services/swan.ts
@@ -1,7 +1,7 @@
import axios from 'axios'
import Config from 'react-native-config'

const swanOAuthURL = Config.SWAN_URL || 'https://dev-api.swanbitcoin.com/'
const swanOAuthURL = Config.SWAN_URL || 'https://login-demo.curity.io/oauth/v2/oauth-token'//'https://dev-api.swanbitcoin.com/'

export const redeemAuthCode = ( data ) =>
axios( {
Expand Down Expand Up @@ -31,7 +31,7 @@ export const linkSwanWallet = ( data ) =>
url: swanOAuthURL.concat( 'wallets?mode=swan' ),
data,
headers: {
Authorization: `Bearer ${data.swanAuthToken}`
Authorization: `Bearer ${data.swanAuthToken}`
}
} )

Expand All @@ -40,6 +40,6 @@ export const syncSwanWallet = ( data ) =>
method: 'get',
url: `${swanOAuthURL}wallets/${data.swanWalletId}?mode=swan`,
headers: {
Authorization: `Bearer ${data.swanAuthToken}`
Authorization: `Bearer ${data.swanAuthToken}`
}
} )
35 changes: 35 additions & 0 deletions src/store/lib/swan.ts
@@ -0,0 +1,35 @@
import crypto from 'crypto'
import base64url from 'base64url'

export const generatePKCEParameters = () => {
console.log( ' made it to generator' )
const code_verifier = generateRandomString( 64 )

const base64Digest = crypto
.createHash( 'sha256' )
.update( code_verifier )
.digest( 'base64' )

const code_challenge = base64url.fromBase64( base64Digest )

const nonce = `${generateRandomNumber ( 11 )}-${generateRandomString( 3 )}`

const state = `${generateRandomNumber ( 11 )}-${generateRandomString( 3 )}`

return {
code_challenge, code_verifier, nonce, state
}
}

const generateRandomString = ( length: number ): string => {
let randomString = ''
const possibleChars = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
for ( let itr = 0; itr < length; itr++ ) {
randomString += possibleChars.charAt(
Math.floor( Math.random() * possibleChars.length ),
)
}
return randomString
}

const generateRandomNumber = ( digits: number ): number => Math.floor( Math.random() * 10**digits )
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -2441,6 +2441,11 @@ base64-js@^1.0.2, base64-js@^1.1.2, base64-js@^1.2.3:
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.3.1.tgz#58ece8cb75dd07e71ed08c736abc5fac4dbf8df1"
integrity sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==

base64url@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/base64url/-/base64url-3.0.1.tgz#6399d572e2bc3f90a9a8b22d5dbb0a32d33f788d"
integrity sha512-ir1UPr3dkwexU7FdV8qBBbNDRUhMmIekYMFZfi+C/sLNnRESKPl23nB9b2pltqfOQNnGzsDdId90AEtG5tCx4A==

base@^0.11.1:
version "0.11.2"
resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f"
Expand Down

0 comments on commit 4ce9006

Please sign in to comment.