Skip to content

Commit

Permalink
Merge pull request #809 from alexstotsky/add-ttl-selection
Browse files Browse the repository at this point in the history
(feature) Auth token TTL customization
  • Loading branch information
ezewer authored May 8, 2024
2 parents 3bc1dec + 9a6e60b commit 3f7e700
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 2 deletions.
10 changes: 10 additions & 0 deletions public/locales/en/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@
"invalidToken": "Invalid token, please try again",
"loginSessionExpired": "Your login session has expired, please try again"
},
"ttlSelect": {
"title": "Auth Token TTL",
"1day": "1 Day",
"2days": "2 Days",
"3days": "3 Days",
"4days": "4 Days",
"5days": "5 Days",
"6days": "6 Days",
"7days": "7 Days"
},
"apiKey": "API Key",
"apiSecret": "API Secret",
"accWithApiKey": "Add account with API key",
Expand Down
7 changes: 7 additions & 0 deletions src/components/Preferences/Preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ThemeSwitcher from 'ui/ThemeSwitcher'
import TimezonePicker from 'ui/TimezonePicker'
import TableScrollPref from 'ui/TableScrollPref'
import ShowMilliseconds from 'ui/ShowMilliseconds'
import TokenTTLSelector from 'ui/TokenTTLSelector'
import DateFormatSelector from 'ui/DateFormatSelector'
import SyncAfterUpdatePref from 'ui/SyncAfterUpdatePref'
import TimeRangePreservePref from 'ui/TimeRangePreservePref'
Expand Down Expand Up @@ -77,6 +78,12 @@ const Preferences = ({
<div>{t('preferences.dateformat')}</div>
<DateFormatSelector />
</div>
{showFrameworkMode && (
<div className='preferences-item'>
<div>{t('auth.ttlSelect.title')}</div>
<TokenTTLSelector />
</div>
)}
</div>
<div className='preferences-row'>
<span>{t('preferences.milliseconds')}</span>
Expand Down
8 changes: 8 additions & 0 deletions src/state/auth/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ export function disableAuthBtn(payload) {
}
}

export function setAuthTokenTTL(payload) {
return {
type: types.SET_TOKEN_TTL,
payload,
}
}

export default {
checkAuth,
addUser,
Expand Down Expand Up @@ -251,4 +258,5 @@ export default {
deleteAccount,
syncAfterUpdate,
disableAuthBtn,
setAuthTokenTTL,
}
2 changes: 2 additions & 0 deletions src/state/auth/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default {
DELETE_ACCOUNT: 'BITFINEX/AUTH/ACCOUNT/DELETE',
SET_SYNC_AFTER_UPDATE: 'BITFINEX/SYNC/SYNC_AFTER_UPDATE/SET',
DISABLE_AUTH_BUTTON: 'BITFINEX/AUTH/BUTTON/DISABLE',
SET_TOKEN_TTL: 'BITFINEX/AUTH/TOKEN_TTL/SET',

WS_SIGN_IN: 'ws_signIn',
LOGIN_2FA_OTP: 'otp',
DEFAULT_TOKEN_TTL: 86400, // 24h
}
6 changes: 6 additions & 0 deletions src/state/auth/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const initialState = {
userShouldReLogin: '',
shouldNotSyncOnStartupAfterUpdate: false,
isAuthBtnDisabled: false,
authTokenTTLSec: types.DEFAULT_TOKEN_TTL,
}

export function authReducer(state = initialState, action) {
Expand Down Expand Up @@ -154,6 +155,11 @@ export function authReducer(state = initialState, action) {
...state,
isAuthBtnDisabled: payload,
}
case types.SET_TOKEN_TTL:
return {
...state,
authTokenTTLSec: payload,
}
case types.HIDE_AUTH:
return {
...state,
Expand Down
19 changes: 19 additions & 0 deletions src/state/auth/saga.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,24 @@ function* handleSyncAfterUpdate({ payload }) {
}
}

function* handleUpdateTokenTTL({ payload }) {
try {
const auth = yield select(selectAuth)
const params = { authTokenTTLSec: payload }
const { error } = yield makeFetchCall('updateUser', params, auth)

if (error) {
yield put(updateErrorStatus({
id: 'status.fail',
topic: 'auth.updateUser',
detail: error?.message ?? JSON.stringify(error),
}))
}
} catch (fail) {
yield put(updateAuthErrorStatus(fail))
}
}

export default function* authSaga() {
yield takeLatest(types.CHECK_AUTH, checkAuth)
yield takeLatest(types.FETCH_USERS, fetchUsers)
Expand All @@ -528,5 +546,6 @@ export default function* authSaga() {
yield takeLatest(types.REMOVE_USER, removeUser)
yield takeLatest(types.AUTH_EXPIRED, handleExpiredAuth)
yield takeLatest(types.SET_SYNC_AFTER_UPDATE, handleSyncAfterUpdate)
yield takeLatest(types.SET_TOKEN_TTL, handleUpdateTokenTTL)
yield fork(tokenRefreshSaga)
}
4 changes: 4 additions & 0 deletions src/state/auth/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import _first from 'lodash/first'
import _filter from 'lodash/filter'
import { isEqual } from '@bitfinex/lib-js-util-base'

import types from './constants'

const getAuth = state => state.auth

export const getAuthStatus = state => getAuth(state).authStatus
Expand All @@ -23,6 +25,7 @@ export const getIsSubAccsAvailable = state => _first(
)?.isApiKeysAuth ?? true
export const getLocalUsername = state => getAuth(state)?.localUsername ?? null
export const getIsAuthBtnDisabled = state => getAuth(state)?.isAuthBtnDisabled ?? false
export const getAuthTokenTTL = state => getAuth(state)?.authTokenTTLSec ?? types.DEFAULT_TOKEN_TTL

export const getAuthData = state => {
const {
Expand Down Expand Up @@ -110,4 +113,5 @@ export default {
getLocalUsername,
getShouldNotSyncOnStartupAfterUpdate,
getIsAuthBtnDisabled,
getAuthTokenTTL,
}
4 changes: 2 additions & 2 deletions src/ui/Select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Select extends PureComponent {
}

itemRenderer = (item, { modifiers, handleClick }) => {
const { active, disabled } = modifiers
const { disabled } = modifiers
const { value } = this.props

let options = {}
Expand All @@ -82,7 +82,7 @@ class Select extends PureComponent {

return (
<MenuItem
active={active}
active={isCurrent}
intent={isCurrent ? Intent.PRIMARY : Intent.NONE}
disabled={disabled}
key={key}
Expand Down
39 changes: 39 additions & 0 deletions src/ui/TokenTTLSelector/TokenTTLSelector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, { useCallback, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import { useDispatch, useSelector } from 'react-redux'

import { setAuthTokenTTL } from 'state/auth/actions'
import { getAuthTokenTTL } from 'state/auth/selectors'

import Select from 'ui/Select'

const getItems = (t) => [
{ value: 86400, label: t('auth.ttlSelect.1day') },
{ value: 172800, label: t('auth.ttlSelect.2days') },
{ value: 259200, label: t('auth.ttlSelect.3days') },
{ value: 345600, label: t('auth.ttlSelect.4days') },
{ value: 432000, label: t('auth.ttlSelect.5days') },
{ value: 518400, label: t('auth.ttlSelect.6days') },
{ value: 604800, label: t('auth.ttlSelect.7days') },
]

const ExportTypeSelector = () => {
const { t } = useTranslation()
const dispatch = useDispatch()
const authTokenTTL = useSelector(getAuthTokenTTL)
const items = useMemo(() => getItems(t), [t])

const handleChange = useCallback((value) => {
dispatch(setAuthTokenTTL(value))
}, [dispatch])

return (
<Select
items={items}
value={authTokenTTL}
onChange={handleChange}
/>
)
}

export default ExportTypeSelector
1 change: 1 addition & 0 deletions src/ui/TokenTTLSelector/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './TokenTTLSelector'

0 comments on commit 3f7e700

Please sign in to comment.