-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Capella iQ Supplemental terms dialog box (#360)
Added a dialog box for the "Accept iQ Supplemental terms" so that the org owner is able to review and accept before proceeding to login. If the user is not a org owner, the user will be asked to check with the org owner and logged out.
- Loading branch information
1 parent
9d00d6c
commit 7f6aa1b
Showing
8 changed files
with
548 additions
and
184 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,106 +1,189 @@ | ||
import { Constants } from "../../util/constants"; | ||
import { Global, Memory } from "../../util/util"; | ||
import { iqRestApiService } from "./iqRestApiService"; | ||
import * as keytar from 'keytar'; | ||
import * as keytar from "keytar"; | ||
|
||
interface IFormData { | ||
username: string; | ||
password: string; | ||
rememberMe: boolean; | ||
} | ||
|
||
type User = { | ||
id: string; | ||
roles: string[]; | ||
}; | ||
|
||
export interface ISavedLoginDataGetter { | ||
doesLoginDetailsExists: boolean; | ||
username: string | ||
username: string; | ||
} | ||
|
||
const getSessionsJwt = async (formData: IFormData): Promise<string> => { | ||
try { | ||
return await iqRestApiService.capellaLogin(formData.username, formData.password); | ||
return await iqRestApiService.capellaLogin( | ||
formData.username, | ||
formData.password | ||
); | ||
} catch (error: any) { | ||
throw new Error(error.message.toString()); | ||
} | ||
}; | ||
|
||
|
||
export const iqLoginHandler = async (formData: IFormData) => { | ||
try { | ||
// Check for remember me | ||
// Check for remember me | ||
if (formData.rememberMe === true) { | ||
Global.state.update(Constants.IQ_USER_ID, formData.username); | ||
keytar.setPassword(Constants.IQ_PASSWORD, formData.username, formData.password); | ||
keytar.setPassword( | ||
Constants.IQ_PASSWORD, | ||
formData.username, | ||
formData.password | ||
); | ||
} | ||
|
||
// Return organization select page data | ||
const jwtToken = await getSessionsJwt(formData); | ||
Memory.state.update("vscode-couchbase.iq.jwtToken", jwtToken); | ||
const organizations = await iqRestApiService.loadOrganizations(jwtToken); | ||
const organizations = await iqRestApiService.loadOrganizations( | ||
jwtToken | ||
); | ||
return organizations; | ||
} catch(error: any){ | ||
} catch (error: any) { | ||
throw new Error(error.message.toString()); | ||
} | ||
}; | ||
|
||
export const iqSavedLoginDataGetter = async (): Promise<ISavedLoginDataGetter> => { | ||
const username = Global.state.get<string>(Constants.IQ_USER_ID); | ||
if (username && username !== "") { | ||
// Username exists, sending it back to webview | ||
return { | ||
doesLoginDetailsExists: true, | ||
username: username | ||
}; | ||
} else { | ||
return { | ||
doesLoginDetailsExists: false, | ||
username: "" | ||
}; | ||
} | ||
}; | ||
export const iqSavedLoginDataGetter = | ||
async (): Promise<ISavedLoginDataGetter> => { | ||
const username = Global.state.get<string>(Constants.IQ_USER_ID); | ||
if (username && username !== "") { | ||
// Username exists, sending it back to webview | ||
return { | ||
doesLoginDetailsExists: true, | ||
username: username, | ||
}; | ||
} else { | ||
return { | ||
doesLoginDetailsExists: false, | ||
username: "", | ||
}; | ||
} | ||
}; | ||
|
||
export const iqSavedLoginHandler = async (username: string) => { | ||
try { | ||
const password = await keytar.getPassword(Constants.IQ_PASSWORD, username); | ||
const password = await keytar.getPassword( | ||
Constants.IQ_PASSWORD, | ||
username | ||
); | ||
if (password) { | ||
// Return organization select page data | ||
const jwtToken = await getSessionsJwt({ | ||
username: username, | ||
password: password, | ||
rememberMe: true | ||
rememberMe: true, | ||
}); | ||
Memory.state.update("vscode-couchbase.iq.jwtToken", jwtToken); | ||
const organizations = await iqRestApiService.loadOrganizations(jwtToken); | ||
const organizations = await iqRestApiService.loadOrganizations( | ||
jwtToken | ||
); | ||
return organizations; | ||
} else { | ||
return undefined; | ||
} | ||
} catch(error: any) { | ||
} catch (error: any) { | ||
throw new Error(error.message.toString()); | ||
} | ||
}; | ||
|
||
export const handleIqSupplementalTerms = async ( | ||
orgDetails: any | ||
): Promise<any> => { | ||
const jwtToken = Memory.state.get<string>("vscode-couchbase.iq.jwtToken"); | ||
if (jwtToken === undefined) { | ||
return { | ||
isOrgVerified: false, | ||
errorMessage: "", | ||
}; | ||
} | ||
if (!orgDetails.iq.other) { | ||
orgDetails.iq.other = {}; | ||
} | ||
// Set the isTermsAcceptedForOrg to True | ||
orgDetails.iq.other.isTermsAcceptedForOrg = true; | ||
const response = await iqRestApiService.acceptIqSupplementalTerms( | ||
jwtToken, | ||
orgDetails.id, | ||
orgDetails | ||
); | ||
}; | ||
|
||
export const checkIfUserIsOrgOwner = async ( | ||
userId: string, | ||
user: User | ||
): Promise<boolean> => { | ||
if (user.id === userId) { | ||
return user.roles.includes("organizationOwner"); | ||
} | ||
return false; | ||
}; | ||
|
||
export const verifyOrganization = async (orgId: string): Promise<any> => { | ||
const jwtToken = Memory.state.get<string>("vscode-couchbase.iq.jwtToken"); | ||
if (jwtToken === undefined) { | ||
return { | ||
shouldAcceptIqTerms: false, | ||
isOrgVerified: false, | ||
errorMessage: "" | ||
errorMessage: "", | ||
}; | ||
} | ||
const orgDetails = await iqRestApiService.getOrganizationDetails(jwtToken, orgId); | ||
if (!orgDetails.iq || orgDetails.iq.enabled === false) { | ||
const orgDetails = await iqRestApiService.getOrganizationDetails( | ||
jwtToken, | ||
orgId | ||
); | ||
const userId = Memory.state.get<string>("vscode-couchbase.iq.userId"); | ||
if (userId === undefined) { | ||
return { | ||
shouldAcceptIqTerms: false, | ||
isOrgVerified: false, | ||
errorMessage: `Capella iQ is not enabled for this organization, Please enable it on cloud.couchbase.com` | ||
errorMessage: "", | ||
}; | ||
} | ||
if (orgDetails.iq.other.isTermsAcceptedForOrg === false) { | ||
if (!orgDetails.iq || orgDetails.iq.enabled === false) { | ||
return { | ||
shouldAcceptIqTerms: false, | ||
isOrgVerified: false, | ||
errorMessage: `Terms and conditions to use Capella iQ are not accepted for this organization, Please accept it on cloud.couchbase.com` | ||
errorMessage: `Capella iQ is not enabled for this organization, Please enable it on cloud.couchbase.com`, | ||
}; | ||
} | ||
if ( | ||
!orgDetails.iq.other || | ||
orgDetails.iq.other.isTermsAcceptedForOrg === false | ||
) { | ||
const userList = await iqRestApiService.fetchUserData( | ||
jwtToken, | ||
orgId, | ||
userId | ||
); | ||
// Allow to Accept Terms ONLY if user is org owner | ||
if (await checkIfUserIsOrgOwner(userId, userList)) { | ||
return { | ||
shouldAcceptIqTerms: true, | ||
isOrgVerified: false, | ||
errorMessage: `Capella iQ uses a third-party large language model (LLM). Please do not enter sensitive data into iQ and review its output before using. `, | ||
}; | ||
} else { | ||
return { | ||
shouldAcceptIqTerms: false, | ||
isOrgVerified: false, | ||
errorMessage: `Capella iQ Supplemental Terms have not yet been accepted. To continue, kindly request the organization owner to review and agree to the terms and conditions. `, | ||
}; | ||
} | ||
} | ||
return { | ||
shouldAcceptIqTerms: false, | ||
isOrgVerified: true, | ||
errorMessage: "" | ||
errorMessage: "", | ||
}; | ||
}; | ||
}; |
Oops, something went wrong.