Last active
September 20, 2021 16:50
-
-
Save jarlah/f109b29b812bcc16f5de2d3799a26571 to your computer and use it in GitHub Desktop.
Test app cognite
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
import { CogniteClient } from '@cognite/sdk'; | |
import { OidcClientCredentials } from './OidcClientCredentials'; | |
import { OIDCClientCredentialsMethod } from "./types/OIDCClientCredentialsMethod" | |
const client = new CogniteClient({ | |
baseUrl: 'https://<CLUSTER>.cognitedata.com', | |
appId: 'test-app' | |
}); | |
// LEGACY REDIRECT | |
// Needs to be used in browser | |
// (async function() { | |
// await client.loginWithOAuth({ | |
// type: 'CDF_OAUTH', | |
// options: { | |
// onAuthenticate: 'REDIRECT' | |
// } | |
// }); | |
// await client.authenticate(); | |
// client.setProject("<PROJECT>"); | |
// // do stuff | |
// }()) | |
(async function() { | |
const accessToken = '<TOKEN>' | |
await client.loginWithCustom(() => { | |
return [() => Promise.resolve(true), accessToken]; | |
}); | |
await client.authenticate(); | |
client.setProject("<PROJECT>"); | |
const list = await client.assets.list(); | |
console.log(list.items.length); | |
}()) | |
export async function authorizeClient(config: { | |
project: string; | |
oidcAppSecret: string; | |
oidcClientId: string; | |
oidcAppId: string; | |
cluster: string; | |
}) { | |
const oidcOptions: OIDCClientCredentialsMethod = { | |
type: "secret", | |
options: { | |
clientSecret: config.oidcAppSecret, | |
clientId: config.oidcClientId, | |
cluster: config.cluster, | |
openIdConfigurationUrl: `https://login.microsoftonline.com/${config.oidcAppId}/V2.0/.well-known/openid-configuration`, | |
scope: `https://${config.cluster}.cognitedata.com/.default` | |
}, | |
}; | |
const client = new CogniteClient({ appId: "test-app" }); | |
client.setProject(config.project); | |
await client.loginWithCustom(async (callbacks) => { | |
return new OidcClientCredentials(oidcOptions, callbacks).init(); | |
}); | |
await client.authenticate(); | |
return client | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment