psnawp_api.core.authenticator module#

Provides Authentication and Authorization classes.

class Authenticator(npsso_cookie: str, common_headers: RequestBuilderHeaders, rate_limit: Rate)[source]#

Bases: object

Provides an interface for PSN Authentication and API.

Variables:
  • npsso_cookie (str) – The NPSSO cookie, which is required for obtaining an access token from the PSN API.

  • common_headers (RequestBuilderHeaders) – Headers that will be passed in each HTTPs request.

  • request_builder (RequestBuilder) – A RequestBuilder object that helps in constructing and sending HTTP requests.

  • token_response (TokenResponse | None) – Stores the token response from PlayStation API to keep track of access token, refresh token, their expiration times.

  • cid (str) – The client ID, a unique identifier for the client, generated using the device’s MAC address.

Note

This class is intended to be used via PSNAWP. See psnawp_api.psnawp.PSNAWP.__init__()

Represents a single authentication to PSN API.

Parameters:
  • npsso_cookie – The Network Platform Single Sign-On (NPSSO) cookie, obtained after signing into PlayStation Network.

  • common_headers – Common headers that will be added to all HTTP request.

  • rate_limit – Controls pacing of HTTP requests to avoid service throttling.

AUTH_HEADER: ClassVar[dict[str, str]] = {'Authorization': 'Basic MDk1MTUxNTktNzIzNy00MzcwLTliNDAtMzgwNmU2N2MwODkxOnVjUGprYTV0bnRCMktxc1A='}#
AUTH_METADATA: ClassVar[dict[str, str]] = {'CLIENT_ID': '09515159-7237-4370-9b40-3806e67c0891', 'REDIRECT_URI': 'com.scee.psxandroid.scecompcall://redirect', 'SCOPE': 'psn:mobile.v2.core psn:clientapp'}#
__init__(npsso_cookie: str, common_headers: RequestBuilderHeaders, rate_limit: Rate) None[source]#

Represents a single authentication to PSN API.

Parameters:
  • npsso_cookie – The Network Platform Single Sign-On (NPSSO) cookie, obtained after signing into PlayStation Network.

  • common_headers – Common headers that will be added to all HTTP request.

  • rate_limit – Controls pacing of HTTP requests to avoid service throttling.

property access_token_expiration_in: int#

Get the time until the access token expires.

If the Authenticator.token_response is not available or expires_in, returns 0.

Returns:

The number of seconds until the access token expires.

property access_token_expiration_time: float#

Get the access token expiration time.

If the Authenticator.token_response is not available or access_token_expires_at, returns current time.

Returns:

The expiration time of the access token as a Unix timestamp.

delete(**kwargs: Unpack[RequestOptions]) Response[source]#

Make a DELETE request with automatic Bearer token authorization.

This method simplifies making POST requests by automatically adding the necessary Authorization header with a Bearer token. You can pass any additional arguments or keyword arguments, which will be forwarded to the underlying request builder’s delete method.

Parameters:

kwargs – Additional arguments to be forwarded to the delete method of the request builder.

Returns:

The response from the POST request.

Raises:

Note

The pre_request_processing() decorator ensures that Authenticator.token_response is usually set correctly. The check for self.token_response is None is a safeguard in case of unexpected issues.

fetch_access_token_from_authorization(authorization_code: str) None[source]#

Obtain the access token using authorization code for the first time, after this the access token is obtained via refresh token.

Parameters:

authorization_code – Code obtained using npsso code.

fetch_access_token_from_refresh() None[source]#

Updates the access token using refresh token.

get(**kwargs: Unpack[RequestOptions]) Response[source]#

Make a GET request with automatic Bearer token authorization.

This method simplifies making GET requests by automatically adding the necessary Authorization header with a Bearer token. You can pass any additional arguments or keyword arguments, which will be forwarded to the underlying request builder’s get method.

Parameters:

kwargs – Additional arguments to be forwarded to the get method of the request builder.

Returns:

The response from the GET request.

Raises:

Note

The pre_request_processing() decorator ensures that Authenticator.token_response is usually set correctly. The check for self.token_response is None is a safeguard in case of unexpected issues.

get_authorization_code() str[source]#

Obtains the authorization code for PSN authentication.

Obtains the access code and the refresh code. Access code lasts about 1 hour. While the refresh code lasts about 2 months. After 2 months a new npsso code is needed.

Raises:

PSNAWPAuthenticationError – If authorization is not successful.

patch(**kwargs: Unpack[RequestOptions]) Response[source]#

Make a PATCH request with automatic Bearer token authorization.

This method simplifies making POST requests by automatically adding the necessary Authorization header with a Bearer token. You can pass any additional arguments or keyword arguments, which will be forwarded to the underlying request builder’s patch method.

Parameters:

kwargs – Additional arguments to be forwarded to the patch method of the request builder.

Returns:

The response from the POST request.

Raises:

Note

The pre_request_processing() decorator ensures that Authenticator.token_response is usually set correctly. The check for self.token_response is None is a safeguard in case of unexpected issues.

post(**kwargs: Unpack[RequestOptions]) Response[source]#

Make a POST request with automatic Bearer token authorization.

This method simplifies making POST requests by automatically adding the necessary Authorization header with a Bearer token. You can pass any additional arguments or keyword arguments, which will be forwarded to the underlying request builder’s post method.

Parameters:

kwargs – Additional arguments to be forwarded to the post method of the request builder.

Returns:

The response from the POST request.

Raises:

Note

The pre_request_processing() decorator ensures that Authenticator.token_response is usually set correctly. The check for self.token_response is None is a safeguard in case of unexpected issues.

put(**kwargs: Unpack[RequestOptions]) Response[source]#

Make a PUT request with automatic Bearer token authorization.

This method simplifies making PUT requests by automatically adding the necessary Authorization header with a Bearer token. You can pass any additional arguments or keyword arguments, which will be forwarded to the underlying request builder’s put method.

Parameters:

kwargs – Additional arguments to be forwarded to the put method of the request builder.

Returns:

The response from the PUT request.

Raises:

Note

The pre_request_processing() decorator ensures that Authenticator.token_response is usually set correctly. The check for self.token_response is None is a safeguard in case of unexpected issues.

property refresh_token_expiration_in: int#

Get the time until the refresh token expires.

If the Authenticator.token_response is not available or refresh_token_expires_in, returns 0.

Returns:

The number of seconds until the refresh token expires.

property refresh_token_expiration_time: float#

Get the refresh token expiration time.

If the Authenticator.token_response is not available or refresh_token_expires_at, returns current time.

Returns:

The expiration time of the refresh token as a Unix timestamp.

class TokenResponse[source]#

Bases: TypedDict

Represents the token response from Sony’s authentication and token refresh APIs.

This includes the access token, refresh token, their expiration times, and additional metadata related to the authorization process.

access_token: str#
access_token_expires_at: NotRequired[float]#
expires_in: int#
id_token: str#
refresh_token: str#
refresh_token_expires_at: NotRequired[float]#
refresh_token_expires_in: int#
scope: str#
token_type: str#
pre_request_processing(method: Callable[PT, RT]) Callable[PT, RT][source]#

A decorator that ensures the authenticator has a valid access token before making an HTTP request.

If the access token is missing or expired, it fetches a new token using the refresh token or npsso cookie.

Parameters:

method – The decorated HTTP request method.

Returns:

The wrapped method with token validation logic applied.