@@ -10,14 +10,15 @@ type Secrets = {
10
10
11
11
// Todo: custom errors
12
12
13
- // We only want to ensure the request originated from our website,
14
- // hence no identifiable information. An in memory array as your request
15
- // should be done pretty fast + we have a maxStates variable.
16
- const states : string [ ] = [ ] ;
17
13
18
14
export class MsAuthClient {
19
15
msAuthEndpoint : string ;
20
16
scopeUrls : string [ ] ;
17
+ statesTail = - 1 ;
18
+ // We only want to ensure the request originated from our website,
19
+ // hence no identifiable information. An in memory array as your request
20
+ // should be done pretty fast + we have a maxStates variable.
21
+ states = [ ] as ( string | undefined ) [ ] ;
21
22
22
23
public constructor (
23
24
private scopes : string [ ] ,
@@ -33,12 +34,8 @@ export class MsAuthClient {
33
34
34
35
public getRedirectUrl ( state ?: string ) : string {
35
36
const _state = state || crypto . randomUUID ( ) ;
36
- const noStates = states . push ( _state ) ;
37
- // Flush the states for performance
38
- if ( noStates > this . maxStates ) {
39
- states . length = 1 ;
40
- states [ 0 ] = _state ;
41
- }
37
+ this . statesTail = ( this . statesTail + 1 ) % this . maxStates ;
38
+ this . states [ this . statesTail ] = _state ;
42
39
43
40
const url = buildUrl ( this . msAuthEndpoint , {
44
41
path : '/authorize' ,
@@ -57,11 +54,11 @@ export class MsAuthClient {
57
54
}
58
55
59
56
public async verifyAndConsumeCode ( code : string , state : string ) {
60
- const index = states . indexOf ( state ) ;
57
+ const index = this . states . indexOf ( state ) ;
61
58
if ( index == - 1 ) {
62
59
throw new Error ( `Failed to verify code: Mismatched state.` ) ;
63
60
}
64
- states . splice ( index , 1 ) ;
61
+ this . states [ index ] = undefined ;
65
62
66
63
const req = await fetch ( `${ this . msAuthEndpoint } /token` , {
67
64
method : 'POST' ,
0 commit comments