Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2fa #749

Open
Strooss opened this issue Jul 4, 2023 · 11 comments
Open

2fa #749

Strooss opened this issue Jul 4, 2023 · 11 comments
Labels
Feature Request a new feature

Comments

@Strooss
Copy link

Strooss commented Jul 4, 2023

Which package is the feature request for?

The core library

Feature

add a way to enable 2fa

Ideal solution or implementation

so the abilty to activate 2fa in the account. it can give the user the access code and with that access you can generate the 2fa code and put it and activate it normally.

let accessCode = client.user.getAccessCode();
await client.enable2fa(function(accessCode))

Alternative solutions or implementations

No response

Other context

No response

@Strooss Strooss added the Feature Request a new feature label Jul 4, 2023
@TheDevYellowy
Copy link
Contributor

The access code is not given by discord it's given by whatever service you use. So you need to do the following

  1. see if the service you use has an api that will give you a code
  2. if it does then you would need to code your own function to get the code

@Strooss
Copy link
Author

Strooss commented Jul 4, 2023 via email

@TheDevYellowy
Copy link
Contributor

TheDevYellowy commented Jul 4, 2023

I don't think any 2fa apps have public apis for security purposes

You could try and reverse engineer their api and see if you can get it that way

@Strooss
Copy link
Author

Strooss commented Jul 4, 2023 via email

@Strooss
Copy link
Author

Strooss commented Jul 4, 2023 via email

@TheDevYellowy
Copy link
Contributor

Ohhhhh, I am currently not home but if someone doesn't give you a response by the time I get home tomorrow I'll take a look at discords api and see if I can get the 2fa token

@Strooss
Copy link
Author

Strooss commented Jul 4, 2023 via email

@TheDevYellowy
Copy link
Contributor

From what I can see there is no way to get the access code via the api you would have to do some web scraping to get it

@aiko-chan-ai
Copy link
Owner

you can only get it when you enable 2fa for the first time (just like you reset bot's token)

@Strooss
Copy link
Author

Strooss commented Jul 14, 2023

you can only get it when you enable 2fa for the first time (just like you reset bot's token)

and how i can get it?

@XielQs
Copy link
Contributor

XielQs commented Feb 5, 2024

Actually you can enable/disable 2FA with node using speakeasy package (or something), here is an example:

const speakeasy = require('speakeasy')
const secret = 'A'.repeat(32) // anything you can want (but it must be 32 char)
const totp = speakeasy.totp({
  secret,
  encoding: 'base32'
})
const response = await axios.post('https://discord.com/api/v9/users/@me/mfa/totp/enable', {
  code: totp,
  secret: secret,
  password: "<DISCORD_PASSWORD>"
}, { headers: { Authorization: '<DISCORD_TOKEN>' } })

HTTP Syntax

POST /api/v9/users/@me/mfa/totp/enable HTTP/1.1
Host: discord.com
Content-Type: application/json
Authorization: <TOKEN>

{
  "code": "<TOTP_CODE>",
  "secret": "<2FA_SECRET_YOU_GENERATED>",
  "password": "<ACCOUNT_PASSWORD>"
}

It returns

{
  "token": "<NEW_DISCORD_TOKEN>",
  "backup_codes": [
    {
      "user_id": "<YOUR_USER_ID>",
      "code": "<BACKUP_CODE>",
      "consumed": false
    },
    ... // it generates 10 backup code
  ]
}

You're old discord token will be invalid after 2FA enabled, if you want to disable it simply just:

POST /api/v9/users/@me/mfa/totp/disable HTTP/1.1
Host: discord.com
Authorization: <TOKEN>

It returns a 401 response like:

HTTP/1.1 401 Unauthorized
Content-Type: application/json

{
    "message": "Two factor is required for this operation",
    "code": 60003,
    "mfa": {
        "ticket": "<TOKEN_TICKET>",
        "methods": [
            {
                "type": "totp",
                "backup_codes_allowed": true
            },
            {
                "type": "backup" // if you have backup keys or something like that idk
            }
        ]
    }
}

You need TOKEN_TICKET to process, next request is:

POST /api/v9/mfa/finish HTTP/1.1
Host: discord.com
Authorization: <TOKEN>
Content-Type: application/json

{
    "ticket": "<TOKEN_TICKET>",
    "mfa_type": "totp", // or another method you want
    "data": "<TOTP_CODE_or_something>"
}

And it responds with a object, that has a token like

{
    "token": "<JWT_TOKEN>"
}

And last step!

POST /api/v9/users/@me/mfa/totp/disable HTTP/1.1
Host: discord.com
Authorization: <TOKEN>
Content-Type: application/json
X-Discord-Mfa-Authorization: <JWT_TOKEN>

{
    "token": "<TOKEN_TICKET>"
}

And if its successful it returns your discord token like

{
    "token": "<YOUR_NEW_DISCORD_TOKEN>"
}

Note: I did NOT tried the 2FA remove method, but it will probably work, and the adding method seems to work without any problems.

Thats all i got :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request a new feature
Projects
None yet
Development

No branches or pull requests

4 participants