mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-08 13:00:05 +00:00
chore(*): refactor, move all auth-related code to auth folder
This commit is contained in:
@@ -1,11 +1,7 @@
|
||||
import axios, { AxiosInstance } from 'axios'
|
||||
import { isAuthorizeFormRequired, parseAndSubmitAuthorizeForm } from '.'
|
||||
import { ServerType } from '../types'
|
||||
import {
|
||||
serialize,
|
||||
isAuthorizeFormRequired,
|
||||
parseAndSubmitAuthorizeForm,
|
||||
isLogInSuccess
|
||||
} from '../utils'
|
||||
import { serialize } from '../utils'
|
||||
|
||||
export class AuthManager {
|
||||
public userName = ''
|
||||
@@ -15,7 +11,7 @@ export class AuthManager {
|
||||
constructor(
|
||||
private serverUrl: string,
|
||||
private serverType: ServerType,
|
||||
private loginCallback: Function
|
||||
private loginCallback: () => Promise<void>
|
||||
) {
|
||||
this.httpClient = axios.create({ baseURL: this.serverUrl })
|
||||
this.loginUrl = `/SASLogon/login`
|
||||
@@ -41,7 +37,7 @@ export class AuthManager {
|
||||
|
||||
const { isLoggedIn, loginForm } = await this.checkSession()
|
||||
if (isLoggedIn) {
|
||||
this.loginCallback()
|
||||
await this.loginCallback()
|
||||
|
||||
return {
|
||||
isLoggedIn,
|
||||
@@ -58,7 +54,10 @@ export class AuthManager {
|
||||
.post<string>(this.loginUrl, loginParamsStr, {
|
||||
withCredentials: true,
|
||||
responseType: 'text',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
Accept: '*/*'
|
||||
}
|
||||
})
|
||||
.then((response) => response.data)
|
||||
|
||||
@@ -159,3 +158,6 @@ export class AuthManager {
|
||||
return this.httpClient.get(this.logoutUrl).then(() => true)
|
||||
}
|
||||
}
|
||||
|
||||
const isLogInSuccess = (response: string): boolean =>
|
||||
/You have signed in/gm.test(response)
|
||||
6
src/auth/index.ts
Normal file
6
src/auth/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { AuthManager } from './AuthManager'
|
||||
|
||||
export * from './AuthManager'
|
||||
export * from './isAuthorizeFormRequired'
|
||||
export * from './isLoginRequired'
|
||||
export * from './parseAndSubmitAuthorizeForm'
|
||||
3
src/auth/isAuthorizeFormRequired.ts
Normal file
3
src/auth/isAuthorizeFormRequired.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export const isAuthorizeFormRequired = (response: string): boolean => {
|
||||
return /<form.+action="(.*Logon\/oauth\/authorize[^"]*).*>/gm.test(response)
|
||||
}
|
||||
5
src/auth/isLoginRequired.ts
Normal file
5
src/auth/isLoginRequired.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export const isLogInRequired = (response: string): boolean => {
|
||||
const pattern: RegExp = /<form.+action="(.*Logon[^"]*).*>/gm
|
||||
const matches = pattern.test(response)
|
||||
return matches
|
||||
}
|
||||
48
src/auth/parseAndSubmitAuthorizeForm.ts
Normal file
48
src/auth/parseAndSubmitAuthorizeForm.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import axios from 'axios'
|
||||
|
||||
export const parseAndSubmitAuthorizeForm = async (
|
||||
response: string,
|
||||
serverUrl: string
|
||||
) => {
|
||||
let authUrl: string | null = null
|
||||
const params: any = {}
|
||||
|
||||
const responseBody = response.split('<body>')[1].split('</body>')[0]
|
||||
const bodyElement = document.createElement('div')
|
||||
bodyElement.innerHTML = responseBody
|
||||
|
||||
const form = bodyElement.querySelector('#application_authorization')
|
||||
authUrl = form ? serverUrl + form.getAttribute('action') : null
|
||||
|
||||
const inputs: any = form?.querySelectorAll('input')
|
||||
|
||||
for (const input of inputs) {
|
||||
if (input.name === 'user_oauth_approval') {
|
||||
input.value = 'true'
|
||||
}
|
||||
|
||||
params[input.name] = input.value
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
|
||||
for (const key in params) {
|
||||
if (params.hasOwnProperty(key)) {
|
||||
formData.append(key, params[key])
|
||||
}
|
||||
}
|
||||
|
||||
if (!authUrl) {
|
||||
throw new Error('Auth Form URL is null or undefined.')
|
||||
}
|
||||
|
||||
return await axios
|
||||
.post(authUrl, formData, {
|
||||
withCredentials: true,
|
||||
responseType: 'text',
|
||||
headers: {
|
||||
Accept: '*/*'
|
||||
}
|
||||
})
|
||||
.then((res) => res.data)
|
||||
}
|
||||
Reference in New Issue
Block a user