-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add auth flows client and server sides
- Loading branch information
Showing
19 changed files
with
409 additions
and
72 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,18 @@ | ||
{ | ||
"//": "private refers to what's internal to snyk, i.e. the snyk.io server", | ||
"private": [ | ||
{ | ||
"//": "send any type of request to our connected clients", | ||
"method": "any", | ||
"path": "/*" | ||
} | ||
], | ||
"public": [ | ||
{ | ||
"//": "send any type of request to our connected clients", | ||
"method": "any", | ||
"path": "/*" | ||
} | ||
] | ||
} | ||
|
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,50 @@ | ||
import { getConfig } from '../../common/config/config'; | ||
import { PostFilterPreparedRequest } from '../../common/relay/prepareRequest'; | ||
import version from '../../common/utils/version'; | ||
import { | ||
HttpResponse, | ||
makeRequestToDownstream, | ||
} from '../../hybrid-sdk/http/request'; | ||
import { Role } from '../types/client'; | ||
|
||
export interface BrokerServerConnectionParams { | ||
connectionIdentifier: string; | ||
brokerClientId: string; | ||
authorization: string; | ||
role: Role; | ||
serverId: number; | ||
} | ||
export const renewBrokerServerConnection = async ( | ||
brokerServerConnectionParams: BrokerServerConnectionParams, | ||
): Promise<HttpResponse> => { | ||
const clientConfig = getConfig(); | ||
const apiHostname = clientConfig.API_BASE_URL; | ||
const body = { | ||
data: { | ||
type: 'broker_connection', | ||
attributes: { | ||
broker_client_id: brokerServerConnectionParams.brokerClientId, | ||
}, | ||
}, | ||
}; | ||
const url = new URL( | ||
`${apiHostname}/hidden/brokers/connections/${brokerServerConnectionParams.connectionIdentifier}/auth/refresh`, | ||
); | ||
url.searchParams.append('role', brokerServerConnectionParams.role); | ||
if (brokerServerConnectionParams.serverId) { | ||
url.searchParams.append( | ||
'serverId', | ||
`${brokerServerConnectionParams.serverId}`, | ||
); | ||
} | ||
const req: PostFilterPreparedRequest = { | ||
url: url.toString(), | ||
headers: { | ||
authorization: brokerServerConnectionParams.authorization, | ||
'user-agent': `Snyk Broker Client ${version}`, | ||
}, | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
}; | ||
return await makeRequestToDownstream(req); | ||
}; |
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
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
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
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
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,42 @@ | ||
import { getConfig } from '../../common/config/config'; | ||
import { PostFilterPreparedRequest } from '../../common/relay/prepareRequest'; | ||
import { makeSingleRawRequestToDownstream } from '../../hybrid-sdk/http/request'; | ||
import { log as logger } from '../../logs/logger'; | ||
|
||
export const validateBrokerClientCredentials = async ( | ||
authHeaderValue: string, | ||
brokerClientId: string, | ||
brokerConnectionIdentifier: string, | ||
) => { | ||
const body = { | ||
data: { | ||
type: 'broker_connection', | ||
attributes: { | ||
broker_client_id: brokerClientId, | ||
}, | ||
}, | ||
}; | ||
|
||
const req: PostFilterPreparedRequest = { | ||
url: `${ | ||
getConfig().apiHostname | ||
}/hidden/brokers/connections/${brokerConnectionIdentifier}/auth/validate?version=2024-02-08~experimental`, | ||
headers: { | ||
authorization: authHeaderValue, | ||
'Content-type': 'application/vnd.api+json', | ||
}, | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
}; | ||
|
||
const response = await makeSingleRawRequestToDownstream(req); | ||
if (response.statusCode === 201) { | ||
return true; | ||
} else { | ||
logger.debug( | ||
{ statusCode: response.statusCode, message: response.statusText }, | ||
`Broker ${brokerConnectionIdentifier} client ID ${brokerClientId} failed validation.`, | ||
); | ||
return false; | ||
} | ||
}; |
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
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,60 @@ | ||
import { Request, Response } from 'express'; | ||
import { validateBrokerClientCredentials } from '../auth/authHelpers'; | ||
import { log as logger } from '../../logs/logger'; | ||
import { validate } from 'uuid'; | ||
import { getSocketConnectionByIdentifier } from '../socket'; | ||
interface BrokerConnectionAuthRequest { | ||
data: { | ||
attributes: { | ||
broker_client_id: string; | ||
}; | ||
id: string; | ||
type: 'broker_connection'; | ||
}; | ||
} | ||
export const authRefreshHandler = async (req: Request, res: Response) => { | ||
const credentials = req.headers['authorization']; | ||
const brokerAppClientId = | ||
req.headers[`${process.env.SNYK_INTERNAL_AUTH_CLIENT_ID_HEADER}`]; | ||
const identifier = req.params.identifier; | ||
const body = JSON.parse(req.body.toString()) as BrokerConnectionAuthRequest; | ||
const brokerClientId = body.data.attributes.broker_client_id; | ||
if ( | ||
!validate(identifier) || | ||
!validate(brokerClientId) || | ||
!validate(brokerAppClientId) | ||
) { | ||
logger.warn( | ||
{ identifier, brokerClientId, brokerAppClientId }, | ||
'Invalid credentials', | ||
); | ||
return res.status(401).send('Invalid parameters or credentials.'); | ||
} | ||
|
||
const connection = getSocketConnectionByIdentifier(identifier); | ||
const currentClient = connection | ||
? connection.find((x) => x.metadata.clientId === brokerClientId) | ||
: null; | ||
logger.debug({ identifier, brokerClientId }, 'Validating credentials'); | ||
if ( | ||
credentials === undefined || | ||
brokerAppClientId === undefined || | ||
credentials?.split('.').length != 3 || | ||
!connection || | ||
!currentClient | ||
) { | ||
return res.status(401).send('Invalid credentials.'); | ||
} else { | ||
const credsCheckResponse = await validateBrokerClientCredentials( | ||
credentials, | ||
brokerClientId as string, | ||
identifier, | ||
); | ||
if (credsCheckResponse) { | ||
return res.status(200).send('OK'); | ||
} else { | ||
currentClient.socket!.end(); | ||
return res.status(401).send('Invalid credentials.'); | ||
} | ||
} | ||
}; |
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
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
Oops, something went wrong.