class splatnet3_scraper.auth.NSO

The NSO class contains all the logic to proceed through the login flow. This class also holds various properties that are used to make requests to the Nintendo Switch Online API. Login flow is roughly as follows, assuming the user has never generated a session token before:

  1. Initialize a requests session and store it.

  2. Generate a random state and S256 code challenge that will be used to obtain the “session_token”. Store them for later use.

  3. Generate a login URL using the state and code challenge that the user will open in their browser. The user will then copy a link and feed it back to the program.

  4. Parse the URI to obtain the session token code, then use it alongside the code challenge to obtain the session_token. Store it for later use. The session token is valid for 2 years and can be revoked by the user.

  5. Use the session token to obtain a user access response from Nintendo. This response will contain an id_token and a user access token. Store both for later user.

  6. Use the user access token to obtain the user information. This is required to obtain the first f_token, for the Nintendo Switch Online API. This is a message authentication code generated from the timestamp and id token in an obscure manner and will be necessary to generate the API access token.

  7. Use the Nintendo-obtained id_token to obtain the first f_token, for the Nintendo Switch Online API. The f_token will also return a timestamp and a request ID.

  8. Use the first f_token, the request ID, the timestamp, the user information, the id_token to obtain the web_service_access_token.

  9. Use the web_service_access_token to obtain the second f_token. This is a message authentication code generated from the timestamp and id token in an obscure manner and will be necessary to generate the gtoken. This step is distinct from step 7, make sure not to use the same f_token twice.

  10. Use the second f_token, the request ID, the timestamp, and the web_service_access_token from step 9, and the id 4834290508791808 to obtain the gtoken.

  11. Use the gtoken and user information to obtain a bullet_token. This token is valid for 2 hours.

Once the login flow is complete, the NSO class contains all the necessary values to regenerate the tokens. To minimize the number of objects to track, the NSO class only stores the tokens in memory. The TokenManager class handles the persistence of the tokens to disk.

Constructors

NSO(session)

Initializes the NSO class. The NSO class contains all the logic and holds all the necessary values to proceed through the login flow.

Methods

g_token_generation_phase_1(id_token, user_info, na_id, f_token_url)

First phase of the gtoken generation process.

g_token_generation_phase_2(web_service_access_token, na_id, ...)

Final phase of the gtoken generation process.

generate_login_url(user_agent=None)

Generates the login URL that can be used to obtain the session token.

generate_new_state()

Generates a new state.

generate_new_verifier()

Generates a new code verifier, which is a random 32 byte string that is base64url encoded and with padding removed. This is used to generate the S256 code challenge.

get_bullet_token(gtoken, user_info, user_agent=None)

Given the gtoken and user information, send a request to SplatNet 3 to obtain the bullet token. This token is required to make any requests to SplatNet 3, and is valid for 6 hours and 30 minutes after it is obtained.

get_ftoken(f_token_url, id_token, step, na_id, coral_user_id=None)

Given the f_token_url, id_token, and step, returns the f_token, request_id, and timestamp from the response.

get_gtoken(session_token, f_token_url=None)

Obtains the gtoken from the session token.

get_gtoken_request(web_service_access_token, f_token, ...)

Given the web_service_access_token, f_token, request_id, and timestamp, returns the gtoken.

get_session_token(session_token_code)

Obtains the session token from the session token code.

get_user_access_token(session_token)

Obtains the user access token from the session token.

get_user_info(user_access_token)

Obtains the user information from the user access token.

get_version()

Fetches the current version of the Nintendo Switch Online app from the iOS app store. This is necessary to access the API. This method retries twice if the version cannot be obtained, in case the iOS App Store site is down or slow. If the version cannot be obtained after three attempts, a fallback version defined in the constants.py file will be used.

get_web_service_access_token(id_token, user_info, f_token, ...)

Given the id_token, user data, f_token, request_id, and timestamp, returns the Web Service Credential Access Token.

static new_instance()

Creates a new instance of the NSO class with a new requests session.

parse_npf_uri(uri)

Parses the uri returned by the Nintendo login page and extracts the session token code. This is used to pass the challenge and verify that the user is who they say they are.

set_new_f_token_function(new_function=None)

Sets the function used to generate the f_token.

Properties

property session_token : str

Returns the session token. This cannot be generated and must be set by the user. If the session token has not been set, a ValueError will be raised.

property splatnet_web_version : str

Get the web view version for SplatNet 3. This is used in the request header key X-Web-View-Ver to indicate the version of the web view that is being used. This is required to make any requests to SplatNet 3. If the version cannot be obtained, a fallback version will be used.

property state : bytes

Returns a base64url encoded random 36 byte string for the auth state. This is used to generate the login URL. To align with node.js’s crypto module, padding is removed. If the state has not been obtained yet, it will be obtained and stored by creating a random 36 byte string and then base64url encoding it.

property verifier : bytes

Returns a base64url encoded random 32 byte string for the code verifier. This is used to generate the S256 code challenge. To align with node.js’s crypto module, padding is removed. If the verifier has not been obtained yet, it will be obtained and stored by creating a random 32 byte string and then base64url encoding it.

property version : str

Returns the current version of the NSO app. Necessary to get the session token. If the version has not been obtained yet, it will be obtained and stored. If the version cannot be obtained, a fallback version will be used.