4.0.7 • Published 2 months ago

@travetto/auth-rest-session v4.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Rest Auth Session

Rest authentication session integration support for the Travetto framework

Install: @travetto/auth-rest-session

npm install @travetto/auth-rest-session

# or

yarn add @travetto/auth-rest-session

One of Rest Auth's main responsibilities is being able to send and receive authentication/authorization information from the client. This data can be encoded in many different forms, and this module provides the ability to encode into and decode from the user's REST Session context. This module fulfills the contract required by Rest Auth of being able to encode and decode a user principal by storing the user principal in the session.

The SessionPrincipalEncoder is exposed as a tool for allowing for decoding/encoding principals into the session.

Code: SessionPrincipalEncoder

import { Injectable, Inject } from '@travetto/di';
import { FilterContext } from '@travetto/rest';
import { Principal } from '@travetto/auth';
import { PrincipalEncoder } from '@travetto/auth-rest';
import { SessionService } from '@travetto/rest-session';

/**
 * Integration with the auth module, using the session as a backing
 * store for the auth principal.
 */
@Injectable()
export class SessionPrincipalEncoder implements PrincipalEncoder {
  #key = '_trv_auth_principal'; // Must be serializable, so it cannot be a symbol

  @Inject()
  service: SessionService;

  encode({ req }: FilterContext, p: Principal): void {
    if (p) {
      p.expiresAt = req.session.expiresAt; // Let principal live as long as the session
      req.session.setValue(this.#key, p);
    } else {
      req.session.destroy(); // Kill session
    }
  }

  async decode({ req }: FilterContext): Promise<Principal | undefined> {
    await this.service.readRequest(req); // Preload session if not already loaded
    return req.session.getValue<Principal>(this.#key);
  }
}

As you can see, encode and decode just read and write from the session context. The main feature here, is that if the authentication expires, the session should be destroyed. Additionally, the user's expiry time is assumed to live as long as the session for simplicity's sake.

4.0.7

2 months ago

4.0.6

2 months ago

4.0.5

2 months ago

4.0.4

2 months ago

4.0.3

2 months ago

4.0.1

3 months ago

4.0.0

3 months ago

4.0.2

3 months ago

4.0.0-rc.10

3 months ago

4.0.0-rc.9

3 months ago

4.0.0-rc.8

3 months ago

3.4.6

3 months ago

4.0.0-rc.7

3 months ago

4.0.0-rc.6

3 months ago

4.0.0-rc.5

3 months ago

4.0.0-rc.3

3 months ago

4.0.0-rc.4

3 months ago

4.0.0-rc.1

3 months ago

4.0.0-rc.2

3 months ago

4.0.0-rc.0

4 months ago

3.4.4

6 months ago

3.4.5

6 months ago

3.3.8

7 months ago

3.3.7

8 months ago

3.3.6

8 months ago

3.4.0

6 months ago

3.2.2

10 months ago

3.2.1

10 months ago

3.4.3

6 months ago

3.2.5

10 months ago

3.4.2

6 months ago

3.2.4

10 months ago

3.4.1

6 months ago

3.2.3

10 months ago

3.4.0-rc.9

6 months ago

3.4.0-rc.7

6 months ago

3.4.0-rc.8

6 months ago

3.4.0-rc.5

6 months ago

3.4.0-rc.6

6 months ago

3.4.0-rc.3

6 months ago

3.4.0-rc.4

6 months ago

3.4.0-rc.1

7 months ago

3.4.0-rc.2

6 months ago

3.4.0-rc.0

7 months ago

3.3.1

9 months ago

3.3.0

9 months ago

3.3.5

8 months ago

3.3.4

8 months ago

3.3.3

8 months ago

3.3.2

9 months ago

3.2.0

11 months ago

3.1.18

11 months ago

3.2.0-rc.0

11 months ago

3.1.12

12 months ago

3.1.11

12 months ago

3.1.14

12 months ago

3.1.13

12 months ago

3.1.16

11 months ago

3.1.15

12 months ago

3.1.17

11 months ago

3.1.10

12 months ago

3.1.9

1 year ago

3.1.8

1 year ago

3.1.7

1 year ago

3.1.6

1 year ago

3.1.5

1 year ago

3.1.4

1 year ago

3.0.3

1 year ago

3.1.0-rc.10

1 year ago

3.1.3

1 year ago

3.1.2

1 year ago

3.1.1

1 year ago

3.1.0

1 year ago

3.1.0-rc.2

1 year ago

3.1.0-rc.3

1 year ago

3.1.0-rc.0

1 year ago

3.1.0-rc.1

1 year ago

3.1.0-rc.6

1 year ago

3.1.0-rc.7

1 year ago

3.1.0-rc.4

1 year ago

3.1.0-rc.5

1 year ago

3.1.0-rc.8

1 year ago

3.1.0-rc.9

1 year ago

3.0.0-rc.26

1 year ago

3.0.0-rc.27

1 year ago

3.0.1-rc.1

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

3.0.2-rc.1

1 year ago

3.0.2-rc.0

1 year ago

3.0.0-rc.24

1 year ago

3.0.0-rc.23

1 year ago

3.0.0-rc.25

1 year ago

3.0.0-rc.20

1 year ago

3.0.0-rc.22

1 year ago

3.0.0-rc.21

1 year ago

3.0.0-rc.15

1 year ago

3.0.0-rc.17

1 year ago

3.0.0-rc.16

1 year ago

3.0.0-rc.19

1 year ago

3.0.0-rc.18

1 year ago

3.0.0-rc.13

1 year ago

3.0.0-rc.12

1 year ago

3.0.0-rc.14

1 year ago

3.0.0-rc.6

1 year ago

3.0.0-rc.11

1 year ago

3.0.0-rc.10

1 year ago

3.0.0-rc.9

1 year ago

3.0.0-rc.8

1 year ago

3.0.0-rc.7

1 year ago

3.0.0-rc.4

2 years ago

3.0.0-rc.3

2 years ago

3.0.0-rc.2

2 years ago

3.0.0-rc.1

2 years ago

3.0.0-rc.0

2 years ago