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

Support for reauthenticating when 2fa fails #120

Open
4e554c4c opened this issue Oct 16, 2020 · 0 comments
Open

Support for reauthenticating when 2fa fails #120

4e554c4c opened this issue Oct 16, 2020 · 0 comments

Comments

@4e554c4c
Copy link

I wrote the following to give this functionality in my bot

import { driver, methodCache, api, settings } from  '@rocket.chat/sdk'
import * as crypto from 'crypto'
api.post_2fa = async (endpoint, data, auth = undefined, ignore = undefined) => {
  return await api.post(endpoint, data, auth, ignore).then((val) => {
    if (val === undefined) {
      // assume our error was TOTP-based (no way to tell because if success=false, undefined is returned)
      // here we mess with the global auth headers
      const old_auth = api.authHeaders 
      api.authHeaders = Object.assign({
        'X-2fa-code': crypto.createHash('sha256').update(settings.password).digest('hex'),
        'X-2fa-method': 'password'
      }, old_auth)
      try {
        return api.post(endpoint, data, auth, ignore)
      } finally {
        api.authHeaders = old_auth
      }
    }
    return val
  })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant