Skip to content
This repository was archived by the owner on Jan 8, 2025. It is now read-only.

Commit f1c69f8

Browse files
authored
Merge pull request #64 from C4IROcean/other/3887_decouple_JS_SDK_from_CDF
Other/3887 decouple js sdk from cdf
2 parents d663d6b + 3352736 commit f1c69f8

File tree

5 files changed

+30
-56
lines changed

5 files changed

+30
-56
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## 0.2.9 (2022-02-15)
1+
## 0.3.0 (2022-09-19)
2+
3+
- Remove dependency on CDF JS SDK
4+
5+
## 0.2.9 (2022-09-15)
26

37
- Deprecate and remove old and unused casts and marine regions functionalities
48

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "odp-sdk",
3-
"version": "0.2.9",
3+
"version": "0.3.0",
44
"description": "Ocean Data Platform JavaScript SDK",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
@@ -29,7 +29,6 @@
2929
"license": "ISC",
3030
"dependencies": {
3131
"@azure/msal-browser": "^2.24.0",
32-
"@cognite/sdk": "^7.3.4",
3332
"loglevel": "^1.8.0",
3433
"eslint-plugin-unicorn": "^43.0.2"
3534
},

source/ODPClient.ts

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,26 @@
11
import { AuthenticationResult, BrowserAuthOptions } from "@azure/msal-browser"
2-
import { ClientOptions, CogniteClient } from "@cognite/sdk"
32
import log from "loglevel"
43
import { Auth } from "./auth"
54
import Catalog from "./Catalog/Catalog"
6-
import { IDataLayer, IDataLayerMain, IDataProduct, IDataProductResult, IIdTokenClaims } from "./types"
7-
8-
// This client id only allows for certain auth_redirects, ideally you'll have a client id per app.
9-
// Contact us if this is your use case.
10-
const ODP_SDK_CLIENT_ID = "b2a2d339-e785-4213-a773-8b289abd2199" // B2C app reg: ODP SDKs
11-
12-
type RequiredConfig = Pick<ClientOptions, "appId">
13-
type OptionalConfig = Partial<Omit<ClientOptions, keyof RequiredConfig | "baseUrl">> &
14-
Partial<{
15-
baseUrl: "https://api.cognitedata.com" | "https://westeurope-1.cognitedata.com"
16-
logLevel: log.LogLevelDesc
17-
}>
18-
19-
interface IAuthTokens {
20-
accessToken: string
21-
idToken: string
22-
idTokenClaims: IIdTokenClaims
23-
authority: string
24-
scopes: string[]
5+
import { IAuthTokens, IDataLayer, IDataLayerMain, IDataProduct, IDataProductResult, IIdTokenClaims } from "./types"
6+
7+
type OdpClientConfig = {
8+
logLevel: log.LogLevelDesc
259
}
2610

2711
type AuthListenersT = (token?: IAuthTokens) => void
2812

29-
const defaultOptions: { project: string; apiKeyMode: boolean; baseUrl: string } = {
30-
project: "odp",
31-
apiKeyMode: false,
32-
baseUrl: "https://api.cognitedata.com",
33-
}
34-
35-
export default class ODPClient extends CogniteClient {
13+
export default class ODPClient {
3614
private authResult: AuthenticationResult | undefined
3715
private listeners: AuthListenersT[] = []
3816

3917
private auth: Auth
4018
private _catalog: Catalog
4119

42-
public constructor(options: RequiredConfig & OptionalConfig, authConfig: BrowserAuthOptions) {
43-
super({
44-
...defaultOptions,
45-
getToken: async () => {
46-
const token = await this.auth.handleRedirectAuth()
47-
if (!token) throw new Error("Could not retrieve token.")
48-
if (this.authResult?.uniqueId !== token?.uniqueId) {
49-
this.authStateUpdated(token)
50-
}
51-
return token.accessToken
52-
},
53-
...options,
54-
})
55-
20+
public constructor(options: OdpClientConfig, authConfig: BrowserAuthOptions) {
5621
log.setLevel(options.logLevel ?? log.levels.SILENT)
5722

58-
this.auth = new Auth(options.baseUrl || defaultOptions.baseUrl, {
23+
this.auth = new Auth({
5924
redirectUri: "http://localhost:3000/", // This should be overwritten!
6025
knownAuthorities: ["oceandataplatform.b2clogin.com"],
6126
authority:
@@ -84,6 +49,14 @@ export default class ODPClient extends CogniteClient {
8449
}
8550
}
8651

52+
public async authenticate() {
53+
if (!this.authResult) {
54+
this.authResult = await this.auth.handleRedirectAuth()
55+
return this.authTokens
56+
}
57+
return this.authTokens
58+
}
59+
8760
public unauthorizeUser() {
8861
return this.auth.logout()
8962
}

source/auth.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@ export type AuthResponseT = AuthenticatedResponseT | UnauthenticatedResponseT
4747
* Auth class
4848
*/
4949
export class Auth {
50-
private audience: string
5150
private _msalInstance: PublicClientApplication
5251

53-
public constructor(audience: string, authConfig: BrowserAuthOptions) {
54-
this.audience = audience
55-
52+
public constructor(authConfig: BrowserAuthOptions) {
5653
this._msalInstance = new PublicClientApplication({
5754
auth: authConfig,
5855
cache: {
@@ -117,13 +114,7 @@ export class Auth {
117114

118115
const accessTokenRequest = {
119116
account,
120-
scopes: [
121-
"openid",
122-
"profile",
123-
"email",
124-
// "offline_access",
125-
`${this.audience}/user_impersonation`,
126-
],
117+
scopes: ["openid", "profile", "email"],
127118
}
128119

129120
const loginWithRedirect = () => {

source/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export interface IIdTokenClaims {
2222
upn: string
2323
ver: string
2424
}
25+
export interface IAuthTokens {
26+
accessToken: string
27+
idToken: string
28+
idTokenClaims: IIdTokenClaims
29+
authority: string
30+
scopes: string[]
31+
}
2532

2633
// types used in app-odp
2734
export enum DataSources {

0 commit comments

Comments
 (0)