1
0
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:
Krishna Acondy
2021-01-24 13:07:15 +00:00
parent e31774ae9d
commit aed39c2ec4
12 changed files with 342 additions and 266 deletions

View File

@@ -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
View File

@@ -0,0 +1,6 @@
import { AuthManager } from './AuthManager'
export * from './AuthManager'
export * from './isAuthorizeFormRequired'
export * from './isLoginRequired'
export * from './parseAndSubmitAuthorizeForm'

View File

@@ -0,0 +1,3 @@
export const isAuthorizeFormRequired = (response: string): boolean => {
return /<form.+action="(.*Logon\/oauth\/authorize[^"]*).*>/gm.test(response)
}

View 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
}

View 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)
}