Skip to content

Commit

Permalink
feat: add debug calls
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 1, 2022
1 parent 49b9cea commit 10b9ed2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 20 deletions.
58 changes: 42 additions & 16 deletions src/clients/oauth1/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
RedirectRequestContract,
} from '../../types.js'

import debug from '../../debug.js'
import { Oauth1Signature } from './signature.js'
import { HttpClient } from '../../http_client.js'
import { UrlBuilder } from '../../url_builder.js'
Expand Down Expand Up @@ -196,18 +197,28 @@ export class Oauth1Client<Token extends Oauth1AccessToken> {
)
}

const requestTokenResponse = await this.makeSignedRequest(
requestTokenUrl,
'post',
undefined,
(request) => {
request.oauth1Param('oauth_callback', this.options.callbackUrl)
this.configureRequestTokenRequest(request)

if (typeof callback === 'function') {
callback(request)
}
}
)
if (debug.enabled) {
debug('oauth1 request token response %o', requestTokenResponse)
}

const {
oauth_token: oauthToken,
oauth_token_secret: oauthTokenSecret,
...parsed
} = await this.makeSignedRequest(requestTokenUrl, 'post', undefined, (request) => {
request.oauth1Param('oauth_callback', this.options.callbackUrl)
this.configureRequestTokenRequest(request)

if (typeof callback === 'function') {
callback(request)
}
})
} = requestTokenResponse

/**
* We expect the response to have "oauth_token" and "oauth_token_secret"
Expand Down Expand Up @@ -254,7 +265,12 @@ export class Oauth1Client<Token extends Oauth1AccessToken> {
callback(urlBuilder)
}

return urlBuilder.makeUrl()
const url = urlBuilder.makeUrl()
if (debug.enabled) {
debug('oauth1 redirect url: "%s"', url)
}

return url
}

/**
Expand Down Expand Up @@ -299,17 +315,27 @@ export class Oauth1Client<Token extends Oauth1AccessToken> {
/**
* Make signed request.
*/
const accessTokenResponse = await this.makeSignedRequest(
accessTokenUrl,
'post',
requestToken,
(request) => {
this.configureAccessTokenRequest(request)

if (typeof callback === 'function') {
callback(request)
}
}
)
if (debug.enabled) {
debug('oauth1 access token response %o', accessTokenResponse)
}

const {
oauth_token: accessOauthToken,
oauth_token_secret: accessOauthTokenSecret,
...parsed
} = await this.makeSignedRequest(accessTokenUrl, 'post', requestToken, (request) => {
this.configureAccessTokenRequest(request)

if (typeof callback === 'function') {
callback(request)
}
})
} = accessTokenResponse

/**
* We expect the response to have "oauth_token" and "oauth_token_secret"
Expand Down
15 changes: 13 additions & 2 deletions src/clients/oauth2/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { HttpClient } from '../../http_client.js'
import { UrlBuilder } from '../../url_builder.js'
import { MissingTokenException } from '../../exceptions/missing_token.js'
import { StateMisMatchException } from '../../exceptions/state_mismatch.js'
import debug from '../../debug.js'

/**
* A generic implementation of OAuth2. One can use it directly with any auth2.0 server
Expand Down Expand Up @@ -119,7 +120,12 @@ export class Oauth2Client<Token extends Oauth2AccessToken> {
callback(urlBuilder)
}

return urlBuilder.makeUrl()
const url = urlBuilder.makeUrl()
if (debug.enabled) {
debug('oauth2 redirect url: "%s"', url)
}

return url
}

/**
Expand Down Expand Up @@ -194,13 +200,18 @@ export class Oauth2Client<Token extends Oauth2AccessToken> {
* Make request and parse response
*/
const response = await httpClient.post()
const accessTokenResponse = this.processClientResponse(httpClient, response)
if (debug.enabled) {
debug('oauth2 access token response %o', accessTokenResponse)
}

const {
access_token: accessToken,
token_type: tokenType,
expires_in: expiresIn,
refresh_token: refreshToken,
...parsed
} = this.processClientResponse(httpClient, response)
} = accessTokenResponse

/**
* We expect the response to have "access_token"
Expand Down
12 changes: 12 additions & 0 deletions src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* @poppinss/oauth-client
*
* (c) Poppinss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { debuglog } from 'node:util'

export default debuglog('oauth_client')
18 changes: 16 additions & 2 deletions src/http_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*/

import got, { type CancelableRequest, type Response } from 'got'

import type { ApiRequestContract } from './types.js'
import debug from './debug.js'

/**
* An HTTP client abstraction we need for making OAuth requests
Expand Down Expand Up @@ -198,13 +200,25 @@ export class HttpClient implements ApiRequestContract {
* Make a post request
*/
async post(): Promise<any> {
return this.#getResponseBody(got.post(this.#baseUrl, this.#getGotOptions('POST')))
const options = this.#getGotOptions('POST')

if (debug.enabled) {
debug('making POST request url: "%s" options: %o', this.#baseUrl, options)
}

return this.#getResponseBody(got.post(this.#baseUrl, options))
}

/**
* Make a get request
*/
async get(): Promise<any> {
return this.#getResponseBody(got.get(this.#baseUrl, this.#getGotOptions('GET')))
const options = this.#getGotOptions('GET')

if (debug.enabled) {
debug('making GET request url: "%s" options:%o', this.#baseUrl, options)
}

return this.#getResponseBody(got.get(this.#baseUrl, options))
}
}

0 comments on commit 10b9ed2

Please sign in to comment.