Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Update renew session response and renew URL (#261)
Browse files Browse the repository at this point in the history
- Renew session endpoint is modified so that we no longer send
request ID in param but as a json object.
- This endpoint now returns a new field sessionExpires. 
This is the absolute time the web session will expire for the most 
recently consumed access request and is the value used in the 
switchback countdown.
- Refactored how bearer token is being set (used type instead of class)
- Update e-ref
  • Loading branch information
kimlisa committed Apr 20, 2021
1 parent 88f2044 commit f8f082c
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 38 deletions.
2 changes: 2 additions & 0 deletions packages/shared/hooks/useAttemptNext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ export type Attempt = {
};

type Callback = (fn?: any) => Promise<any>;

export type State = ReturnType<typeof useAttemptNext>;
11 changes: 7 additions & 4 deletions packages/teleport/src/Main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,34 @@ export function Main(props: State) {
<RouterDOM.Switch>
<Redirect exact={true} from={cfg.routes.root} to={indexRoute} />
</RouterDOM.Switch>
<VerticalSplit>
<StyledMain>
<SideNav />
<HorizontalSplit>
<TopBar />
<Switch>{$features}</Switch>
</HorizontalSplit>
</VerticalSplit>
</StyledMain>
</>
);
}

const VerticalSplit = styled.div`
export const StyledMain = styled.div`
width: 100%;
height: 100%;
display: flex;
flex: 1;
position: absolute;
min-width: 900px;
min-width: 1000px;
`;

const HorizontalSplit = styled.div`
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
// Allows shrinking beyond content size on flexed childrens.
min-width: 0;
`;

const StyledIndicator = styled(HorizontalSplit)`
Expand Down
3 changes: 2 additions & 1 deletion packages/teleport/src/Main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

import { hot } from 'react-hot-loader/root';
import Main from './Main';
import Main, { StyledMain } from './Main';

export { StyledMain };
export default hot(Main);
6 changes: 3 additions & 3 deletions packages/teleport/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const cfg = {
clusterEventsPath: `/v1/webapi/sites/:clusterId/events/search?from=:start?&to=:end?&limit=:limit?`,
scp:
'/v1/webapi/sites/:clusterId/nodes/:serverId/:login/scp?location=:location&filename=:filename',
renewTokenPath: '/v1/webapi/sessions/renew/:requestId?',
renewTokenPath: '/v1/webapi/sessions/renew',
resetPasswordTokenPath: '/v1/webapi/users/password/token',
sessionPath: '/v1/webapi/sessions',
userContextPath: '/v1/webapi/sites/:clusterId/context',
Expand Down Expand Up @@ -266,8 +266,8 @@ const cfg = {
});
},

getRenewTokenUrl(requestId?: string) {
return generatePath(cfg.api.renewTokenPath, { requestId });
getRenewTokenUrl() {
return cfg.api.renewTokenPath;
},

getGithubConnectorsUrl(name?: string) {
Expand Down
3 changes: 2 additions & 1 deletion packages/teleport/src/services/localStorage/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { BearerToken, KeysEnum } from './types';
import { BearerToken } from 'teleport/services/session';
import { KeysEnum } from './types';

const storage = {
clear() {
Expand Down
13 changes: 1 addition & 12 deletions packages/teleport/src/services/localStorage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,5 @@ limitations under the License.
export const KeysEnum = {
TOKEN: 'grv_teleport_token',
TOKEN_RENEW: 'grv_teleport_token_renew',
RELOAD_TABS: 'grv_reload_tabs',
};

export class BearerToken {
accessToken: string;
expiresIn: string;
created: number;

constructor(json) {
this.accessToken = json.token;
this.expiresIn = json.expires_in;
this.created = new Date().getTime();
}
}
2 changes: 2 additions & 0 deletions packages/teleport/src/services/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
*/

import session from './session';

export * from './types';
export default session;
31 changes: 31 additions & 0 deletions packages/teleport/src/services/session/makeSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright 2021 Gravitational, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Session, BearerToken } from './types';

export function makeSession(json: any): Session {
return {
token: makeBearerToken(json),
expires: json.sessionExpires,
};
}

export function makeBearerToken(json: any): BearerToken {
return {
accessToken: json.token,
expiresIn: json.expires_in,
created: new Date().getTime(),
};
}
33 changes: 17 additions & 16 deletions packages/teleport/src/services/session/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import Logger from 'shared/libs/logger';
import cfg from 'teleport/config';
import history from 'teleport/services/history';
import api from 'teleport/services/api';
import localStorage, {
KeysEnum,
BearerToken,
} from 'teleport/services/localStorage';
import localStorage, { KeysEnum } from 'teleport/services/localStorage';
import { makeSession, makeBearerToken } from './makeSession';
import { RenewSessionRequest } from './types';

// Time to determine when to renew session which is
// when expiry time of token is less than 3 minutes.
const RENEW_TOKEN_TIME = 180 * 1000
const RENEW_TOKEN_TIME = 180 * 1000;
const TOKEN_CHECKER_INTERVAL = 15 * 1000; // every 15 sec
const logger = Logger.create('services/session');

Expand Down Expand Up @@ -68,8 +67,10 @@ const session = {
}
},

renewSession(requestId: string) {
return this._renewToken(requestId);
// renewSession renews session and returns the
// absolute time the new session expires.
renewSession(req: RenewSessionRequest): Promise<Date> {
return this._renewToken(req);
},

isValid() {
Expand Down Expand Up @@ -104,7 +105,7 @@ const session = {
el.parentNode.removeChild(el);
const decoded = window.atob(el.content);
const json = JSON.parse(decoded);
return new BearerToken(json);
return makeBearerToken(json);
},

_shouldRenewToken() {
Expand All @@ -119,21 +120,21 @@ const session = {
return this._timeLeft() < RENEW_TOKEN_TIME;
},

_renewToken(requestId?: string) {
_renewToken(req: RenewSessionRequest) {
this._setAndBroadcastIsRenewing(true);
return api
.post(cfg.getRenewTokenUrl(requestId))
.then(this._receiveBearerToken.bind(this))
.post(cfg.getRenewTokenUrl(), req)
.then(res => {
const session = makeSession(res);
localStorage.setBearerToken(session.token);

return session.expires;
})
.finally(() => {
this._setAndBroadcastIsRenewing(false);
});
},

_receiveBearerToken(json) {
const token = new BearerToken(json);
localStorage.setBearerToken(token);
},

_setAndBroadcastIsRenewing(value) {
this._setIsRenewing(value);
localStorage.broadcast(KeysEnum.TOKEN_RENEW, value);
Expand Down
15 changes: 15 additions & 0 deletions packages/teleport/src/services/session/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type RenewSessionRequest = {
requestId?: string;
switchback?: boolean;
};

export type BearerToken = {
accessToken: string;
expiresIn: string;
created: number;
};

export type Session = {
token: BearerToken;
expires: Date;
};
2 changes: 1 addition & 1 deletion packages/webapps.e

0 comments on commit f8f082c

Please sign in to comment.