-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added zendesk oauth endpoints (#166)
- Loading branch information
1 parent
d64af29
commit 82b4340
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import axios from 'axios'; | ||
import { DataObject, OAuthResponse } from '../../lib/types'; | ||
|
||
export const init = async ({ body }: DataObject): Promise<OAuthResponse> => { | ||
try { | ||
const { | ||
clientId: client_id, | ||
clientSecret: client_secret, | ||
metadata: { | ||
code, | ||
formData: { ZENDESK_SUBDOMAIN }, | ||
redirectUri: redirect_uri, | ||
}, | ||
} = body; | ||
|
||
const requestBody = { | ||
grant_type: 'authorization_code', | ||
code, | ||
client_id, | ||
client_secret, | ||
redirect_uri, | ||
scope: 'read write', | ||
code_verifier: | ||
'eNmtK0mRHQ.-93QxgrniB7rb.-TZ_Tjbonygt2aqk1.ltiwXQHmNFeFJPh19MZwXzFDfmFMpUZbAFVtSAtChNU08R4txdDBi7EY6ZHiBp6I8F1drUHZR', | ||
}; | ||
|
||
const response = await axios.post( | ||
`https://${ZENDESK_SUBDOMAIN}.zendesk.com/oauth/tokens`, | ||
requestBody, | ||
); | ||
|
||
const { | ||
data: { token_type: tokenType, access_token: accessToken }, | ||
} = response; | ||
|
||
const profileResponse = await axios.get( | ||
`https://${ZENDESK_SUBDOMAIN}.zendesk.com/api/v2/users/me.json`, | ||
{ headers: { Authorization: `Bearer ${accessToken}` } }, | ||
); | ||
const { | ||
data: { | ||
user: { email: ZENDESK_EMAIL_ADDRESS }, | ||
}, | ||
} = profileResponse; | ||
|
||
return { | ||
accessToken, | ||
refreshToken: accessToken, | ||
expiresIn: 2147483647, | ||
tokenType, | ||
meta: { | ||
ZENDESK_EMAIL_ADDRESS, | ||
}, | ||
}; | ||
} catch (error) { | ||
throw new Error(`Error fetching access token for Zendesk: ${error}`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { DataObject, OAuthResponse } from '../../lib/types'; | ||
|
||
export const refresh = async ({ body }: DataObject): Promise<OAuthResponse> => { | ||
try { | ||
const { | ||
OAUTH_METADATA: { accessToken, refreshToken, tokenType, meta }, | ||
} = body; | ||
return { | ||
accessToken, | ||
refreshToken, | ||
expiresIn: 2147483647, | ||
tokenType, | ||
meta, | ||
}; | ||
} catch (error) { | ||
throw new Error(`Error fetching refresh token for Zendesk: ${error}`); | ||
} | ||
}; |