Skip to content

Commit 51ccf49

Browse files
101arrowzbmeck
andauthored
Login + sync (#12)
* wip login * fix * use remote settings * remove redundant settings * use latest API changes * fix types * working login settings sync * patches Co-authored-by: Bradley Farias <[email protected]> --------- Co-authored-by: Bradley Farias <[email protected]>
1 parent ddc096f commit 51ccf49

File tree

9 files changed

+387
-99
lines changed

9 files changed

+387
-99
lines changed

package.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@
5656
"configuration": {
5757
"title": "Socket Security",
5858
"properties": {
59-
"socket-security.showAllIssueTypes": {
60-
"order": 0,
61-
"type": "boolean",
62-
"default": false,
63-
"description": "Show all issue types, even issues hidden by default."
64-
},
6559
"socket-security.minIssueLevel": {
6660
"order": 1,
6761
"type": "string",
@@ -72,7 +66,7 @@
7266
"middle",
7367
"low"
7468
],
75-
"description": "Hide all issues that are less important than this level, note some issues are hidden by default so you may also wish to enable showing all issue types still."
69+
"description": "Hide all issues that are less important than this level. Note some issues are hidden by default so you may also wish to enable showing all issue types still."
7670
},
7771
"socket-security.errorOverlayThreshold": {
7872
"order": 2,
@@ -91,7 +85,7 @@
9185
"default": 80,
9286
"minimum": 0,
9387
"maximum": 100,
94-
"description": "Show error overlay for any import of a package with a summary score less than this value.",
88+
"description": "Show overlay for any import of a package with a summary score less than this value.",
9589
"examples": [
9690
80
9791
]

src/data/editor-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as vscode from 'vscode'
55
*
66
* ```js
77
* const api = activate(ctx)
8-
* const [minIssueLevel, showAllIssueTypes] = api.getConfigValues([`${EXTENSION_PREFIX}.minIssueLevel`, `${EXTENSION_PREFIX}.showAllIssueTypes`])
8+
* const [minIssueLevel, pythonInterpreter] = api.getConfigValues([`${EXTENSION_PREFIX}.minIssueLevel`, `${EXTENSION_PREFIX}.pythonInterpreter`])
99
* ```
1010
* @param context
1111
* @returns

src/data/github.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function orgOrUserFromString(url: string): string | undefined {
1818
* install the github app against rather than asking for too much permissions
1919
* @param workspaceRootURI
2020
*/
21-
async function sniffForGithubOrgOrUser(workspaceRootURI: vscode.Uri): Promise<string | undefined> {
21+
export async function sniffForGithubOrgOrUser(workspaceRootURI: vscode.Uri): Promise<string | undefined> {
2222
// package.json repository
2323
try {
2424
const pkg = JSON.parse(

src/data/report.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { EXTENSION_PREFIX, addDisposablesTo, getWorkspaceFolderURI, WorkspaceDat
1212
import * as stableStringify from 'safe-stable-stringify';
1313
import watch, { SharedFilesystemWatcherHandler } from '../fs-watch'
1414
import { GlobPatterns, getGlobPatterns } from './glob-patterns';
15-
import { getStaticTOMLValue, parseTOML } from "toml-eslint-parser";
15+
import { getStaticTOMLValue, parseTOML } from 'toml-eslint-parser';
16+
import * as socketAPIConfig from './socket-api-config'
1617

1718
export type SocketReport = {
1819
issues: Array<{
@@ -88,28 +89,10 @@ export async function activate(context: vscode.ExtensionContext, disposables?: A
8889
}
8990
const { workspace } = vscode
9091

91-
const editorConfig = workspace.getConfiguration(EXTENSION_PREFIX)
92-
let apiKey: string | undefined
93-
let authorizationHeaderValue: string = ''
94-
function syncWorkspaceConfiguration() {
95-
// early adopter release given big quota
96-
// hidden settings for testing
97-
apiKey = editorConfig.get('socketSecurityAPIKey') ?? 'sktsec_t_--RAN5U4ivauy4w37-6aoKyYPDt5ZbaT5JBVMqiwKo_api'
98-
if (typeof apiKey !== 'string' || !apiKey) {
99-
apiKey = process.env.SOCKET_SECURITY_API_KEY
100-
}
101-
if (apiKey) {
102-
authorizationHeaderValue = `Basic ${Buffer.from(`${apiKey}:`).toString('base64url')}`
103-
}
104-
reportData.recalculateAll()
105-
}
106-
workspace.onDidChangeConfiguration((e) => {
107-
if (
108-
e.affectsConfiguration(`${EXTENSION_PREFIX}.socketSecurityAPIKey`)
109-
) {
110-
syncWorkspaceConfiguration()
111-
}
112-
})
92+
addDisposablesTo(
93+
disposables,
94+
socketAPIConfig.onAPIConfChange(() => reportData.recalculateAll())
95+
)
11396

11497
const reportWatcher: SharedFilesystemWatcherHandler = {
11598
onDidChange(uri) {
@@ -228,15 +211,41 @@ export async function activate(context: vscode.ExtensionContext, disposables?: A
228211
return vscode.Uri.joinPath(uri, '..').fsPath;
229212
}
230213

214+
let warnedLogin = false
215+
231216
async function runReport(uri: vscode.Uri, force: boolean = false) {
232217
if (!force) {
233218
if (!vscode.workspace.getConfiguration(EXTENSION_PREFIX).get('reportsEnabled')) {
234219
return
235220
}
221+
const result = await socketAPIConfig.getExistingAPIConfig()
222+
if (!result) {
223+
if (!warnedLogin) {
224+
warnedLogin = true
225+
const realLogin = 'Log in'
226+
const publicLogin = 'Use public token'
227+
const res = await vscode.window.showErrorMessage(
228+
'Please log into Socket or use the free, public demo to run reports on your dependency tree.',
229+
realLogin,
230+
publicLogin
231+
)
232+
if (res === publicLogin) {
233+
await socketAPIConfig.usePublicConfig(true)
234+
} else if (res === realLogin) {
235+
await socketAPIConfig.getAPIConfig(true)
236+
}
237+
}
238+
239+
if (!(await socketAPIConfig.getExistingAPIConfig())) {
240+
return
241+
}
242+
}
236243
}
237-
if (!apiKey) {
244+
const apiConfig = await socketAPIConfig.getAPIConfig()
245+
if (!apiConfig) {
238246
return
239247
}
248+
const authorizationHeaderValue = socketAPIConfig.toAuthHeader(apiConfig.apiKey)
240249
const workspaceFolderURI = getWorkspaceFolderURI(uri)
241250
if (!workspaceFolderURI) {
242251
return
@@ -385,7 +394,7 @@ export async function activate(context: vscode.ExtensionContext, disposables?: A
385394
(uri) => runReport(uri),
386395
() => getDefaultReport()
387396
)
388-
syncWorkspaceConfiguration()
397+
reportData.recalculateAll()
389398
const api = {
390399
effectiveReportForUri: (uri: vscode.Uri) => reportData.get(uri),
391400
onReport(...params: Parameters<typeof reportData.on>) {

0 commit comments

Comments
 (0)