psnawp_api.core.authenticator module#
Provides Authentication and Authorization classes.
- class Authenticator(npsso_cookie: str, common_headers: RequestBuilderHeaders, rate_limit: Rate)[source]#
Bases:
objectProvides 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
RequestBuilderobject 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_responseis not available orexpires_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_responseis not available oraccess_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
deletemethod.- Parameters:
kwargs – Additional arguments to be forwarded to the
deletemethod of the request builder.- Returns:
The response from the POST request.
- Raises:
PSNAWPAuthenticationError – If the
Authenticator.token_responseisNone, indicating that an attempt to make an HTTP request was made without an access token.PSNAWPBadRequestError – If the HTTP response status code is 400.
PSNAWPUnauthorizedError – If the HTTP response status code is 401.
PSNAWPForbiddenError – If the HTTP response status code is 403.
PSNAWPNotFoundError – If the HTTP response status code is 404.
PSNAWPNotAllowedError – If the HTTP response status code is 405.
PSNAWPTooManyRequestsError – If the HTTP response status code is 429.
PSNAWPClientError – If the HTTP response status code is in the 4xx range (excluding those listed above).
PSNAWPServerError – If the HTTP response status code is 500 or above.
Note
The
pre_request_processing()decorator ensures thatAuthenticator.token_responseis usually set correctly. The check forself.token_response is Noneis 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.
- 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
getmethod.- Parameters:
kwargs – Additional arguments to be forwarded to the
getmethod of the request builder.- Returns:
The response from the GET request.
- Raises:
PSNAWPAuthenticationError – If the
Authenticator.token_responseisNone, indicating that an attempt to make an HTTP request was made without an access token.PSNAWPBadRequestError – If the HTTP response status code is 400.
PSNAWPUnauthorizedError – If the HTTP response status code is 401.
PSNAWPForbiddenError – If the HTTP response status code is 403.
PSNAWPNotFoundError – If the HTTP response status code is 404.
PSNAWPNotAllowedError – If the HTTP response status code is 405.
PSNAWPTooManyRequestsError – If the HTTP response status code is 429.
PSNAWPClientError – If the HTTP response status code is in the 4xx range (excluding those listed above).
PSNAWPServerError – If the HTTP response status code is 500 or above.
Note
The
pre_request_processing()decorator ensures thatAuthenticator.token_responseis usually set correctly. The check forself.token_response is Noneis 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
patchmethod.- Parameters:
kwargs – Additional arguments to be forwarded to the
patchmethod of the request builder.- Returns:
The response from the POST request.
- Raises:
PSNAWPAuthenticationError – If the
Authenticator.token_responseisNone, indicating that an attempt to make an HTTP request was made without an access token.PSNAWPBadRequestError – If the HTTP response status code is 400.
PSNAWPUnauthorizedError – If the HTTP response status code is 401.
PSNAWPForbiddenError – If the HTTP response status code is 403.
PSNAWPNotFoundError – If the HTTP response status code is 404.
PSNAWPNotAllowedError – If the HTTP response status code is 405.
PSNAWPTooManyRequestsError – If the HTTP response status code is 429.
PSNAWPClientError – If the HTTP response status code is in the 4xx range (excluding those listed above).
PSNAWPServerError – If the HTTP response status code is 500 or above.
Note
The
pre_request_processing()decorator ensures thatAuthenticator.token_responseis usually set correctly. The check forself.token_response is Noneis 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
postmethod.- Parameters:
kwargs – Additional arguments to be forwarded to the
postmethod of the request builder.- Returns:
The response from the POST request.
- Raises:
PSNAWPAuthenticationError – If the
Authenticator.token_responseisNone, indicating that an attempt to make an HTTP request was made without an access token.PSNAWPBadRequestError – If the HTTP response status code is 400.
PSNAWPUnauthorizedError – If the HTTP response status code is 401.
PSNAWPForbiddenError – If the HTTP response status code is 403.
PSNAWPNotFoundError – If the HTTP response status code is 404.
PSNAWPNotAllowedError – If the HTTP response status code is 405.
PSNAWPTooManyRequestsError – If the HTTP response status code is 429.
PSNAWPClientError – If the HTTP response status code is in the 4xx range (excluding those listed above).
PSNAWPServerError – If the HTTP response status code is 500 or above.
Note
The
pre_request_processing()decorator ensures thatAuthenticator.token_responseis usually set correctly. The check forself.token_response is Noneis 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
putmethod.- Parameters:
kwargs – Additional arguments to be forwarded to the
putmethod of the request builder.- Returns:
The response from the PUT request.
- Raises:
PSNAWPAuthenticationError – If the
Authenticator.token_responseisNone, indicating that an attempt to make an HTTP request was made without an access token.PSNAWPBadRequestError – If the HTTP response status code is 400.
PSNAWPUnauthorizedError – If the HTTP response status code is 401.
PSNAWPForbiddenError – If the HTTP response status code is 403.
PSNAWPNotFoundError – If the HTTP response status code is 404.
PSNAWPNotAllowedError – If the HTTP response status code is 405.
PSNAWPTooManyRequestsError – If the HTTP response status code is 429.
PSNAWPClientError – If the HTTP response status code is in the 4xx range (excluding those listed above).
PSNAWPServerError – If the HTTP response status code is 500 or above.
Note
The
pre_request_processing()decorator ensures thatAuthenticator.token_responseis usually set correctly. The check forself.token_response is Noneis a safeguard in case of unexpected issues.
- class TokenResponse[source]#
Bases:
TypedDictRepresents 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_expires_at: NotRequired[float]#
- refresh_token_expires_at: NotRequired[float]#
- 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.