Skip to content

Commit

Permalink
fix: ensure updates made to field values are saved
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 committed Mar 14, 2024
1 parent 389d428 commit 7bd4692
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 30 deletions.
Expand Up @@ -75,6 +75,7 @@ import { clone } from "lodash-es"
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
import { PersistenceService } from "~/services/persistence"
import { useService } from "dioc/vue"
import { grantTypesInvolvingRedirect } from "~/services/oauth/oauth.service"
const persistenceService = useService(PersistenceService)
const t = useI18n()
Expand Down Expand Up @@ -180,6 +181,14 @@ const onGenerateOAuthToken = () => {
const persistedOAuthConfig = JSON.parse(localOAuthTempConfig)
// Only Grant Types involving redirects require persisting associated information
if (
persistedOAuthConfig.grant_type &&
!grantTypesInvolvingRedirect.includes(persistedOAuthConfig.grant_type)
) {
return
}
// Write the collection object to `localStorage` to retrieve later while redirecting back post the OAuth flow
if (persistedOAuthConfig.context.type === "collection-properties") {
persistenceService.setLocalConfig(
Expand Down
Expand Up @@ -246,10 +246,18 @@ onMounted(() => {
const persistedOAuthConfig = JSON.parse(localOAuthTempConfig)
if (persistedOAuthConfig.source === "REST") {
return
}
// If returning from an OAuth redirect, retrieve the persisted information in `localStorage` and display the collection properties modal
if (persistedOAuthConfig.context.type === "collection-properties") {
const { collection, collectionID } = persistedOAuthConfig.context.metadata
if (!collection || !collectionID) {
return
}
const parentCollectionID = collectionID.split("/").slice(0, -1).join("/") // remove last folder to get parent folder
let inheritedProperties = {
Expand Down
10 changes: 9 additions & 1 deletion packages/hoppscotch-common/src/components/collections/index.vue
Expand Up @@ -350,10 +350,18 @@ onMounted(() => {
const persistedOAuthConfig = JSON.parse(localOAuthTempConfig)
if (persistedOAuthConfig.source === "GraphQL") {
return
}
// If returning from an OAuth redirect, retrieve the persisted information in `localStorage` and display the collection properties modal
if (persistedOAuthConfig.context.type === "collection-properties") {
if (persistedOAuthConfig.context?.type === "collection-properties") {
const { collection, collectionID } = persistedOAuthConfig.context.metadata
if (!collection || !collectionID) {
return
}
const parentCollectionID = collectionID.split("/").slice(0, -1).join("/") // remove last folder to get parent folder
let inheritedProperties = {
Expand Down
10 changes: 6 additions & 4 deletions packages/hoppscotch-common/src/components/http/Authorization.vue
Expand Up @@ -179,11 +179,14 @@
</div>
<div v-if="auth.authType === 'oauth-2'" class="w-full">
<div class="flex flex-1 border-b border-dividerLight">
<!-- AM-COMMENT: look into this -->
<!-- Ensure a new object is assigned here to avoid reactivity issues -->
<SmartEnvInput
v-model="auth.grantTypeInfo.token"
:model-value="auth.grantTypeInfo.token"
placeholder="Token"
:envs="envs"
@update:model-value="
auth.grantTypeInfo = { ...auth.grantTypeInfo, token: $event }
"
/>
</div>
<HttpOAuth2Authorization
Expand Down Expand Up @@ -282,11 +285,10 @@ onMounted(() => {
const persistedOAuthConfig = JSON.parse(localOAuthTempConfig)
if (persistedOAuthConfig.context.type === "collection-properties") {
if (persistedOAuthConfig.context?.type === "collection-properties") {
auth.value = <HoppRESTAuth>{
...auth.value,
authType: "oauth-2",
// Replace any values with the associated env var key if applicable
grantTypeInfo: {
...persistedOAuthConfig,
grantType: persistedOAuthConfig.grant_type,
Expand Down
114 changes: 93 additions & 21 deletions packages/hoppscotch-common/src/components/http/OAuth2Authorization.vue
Expand Up @@ -215,6 +215,7 @@ import passwordFlow, {
PasswordFlowParams,
getDefaultPasswordFlowParams,
} from "~/services/oauth/flows/password"
import { grantTypesInvolvingRedirect } from "~/services/oauth/oauth.service"
import { PersistenceService } from "~/services/persistence"
import { GQLTabService } from "~/services/tab/graphql"
import { RESTTabService } from "~/services/tab/rest"
Expand Down Expand Up @@ -256,7 +257,10 @@ const supportedGrantTypes = [
const authEndpoint = refWithCallbackOnChange(
grantType?.authEndpoint,
(value) => {
auth.value.grantTypeInfo.authEndpoint = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
authEndpoint: value,
}
}
)
Expand All @@ -267,12 +271,18 @@ const supportedGrantTypes = [
return
}
auth.value.grantTypeInfo.tokenEndpoint = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
tokenEndpoint: value,
}
}
)
const clientID = refWithCallbackOnChange(grantType?.clientID, (value) => {
auth.value.grantTypeInfo.clientID = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
clientID: value,
}
})
const clientSecret = refWithCallbackOnChange(
Expand All @@ -282,14 +292,20 @@ const supportedGrantTypes = [
return
}
auth.value.grantTypeInfo.clientSecret = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
clientSecret: value,
}
}
)
const scopes = refWithCallbackOnChange(
grantType?.scopes ? grantType.scopes : undefined,
(value) => {
auth.value.grantTypeInfo.scopes = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
scopes: value,
}
}
)
Expand All @@ -300,7 +316,10 @@ const supportedGrantTypes = [
return
}
auth.value.grantTypeInfo.isPKCE = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
isPKCE: value,
}
}
)
Expand All @@ -322,7 +341,10 @@ const supportedGrantTypes = [
return
}
auth.value.grantTypeInfo.codeVerifierMethod = value.id
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
codeVerifierMethod: value.id,
}
}
)
Expand Down Expand Up @@ -442,14 +464,20 @@ const supportedGrantTypes = [
const authEndpoint = refWithCallbackOnChange(
grantTypeInfo?.authEndpoint,
(value) => {
auth.value.grantTypeInfo.authEndpoint = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
authEndpoint: value,
}
}
)
const clientID = refWithCallbackOnChange(
grantTypeInfo?.clientID,
(value) => {
auth.value.grantTypeInfo.clientID = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
clientID: value,
}
}
)
Expand All @@ -460,14 +488,20 @@ const supportedGrantTypes = [
return
}
auth.value.grantTypeInfo.clientSecret = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
clientSecret: value,
}
}
)
const scopes = refWithCallbackOnChange(
grantTypeInfo?.scopes ? grantTypeInfo.scopes : undefined,
(value) => {
auth.value.grantTypeInfo.scopes = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
scopes: value,
}
}
)
Expand Down Expand Up @@ -547,14 +581,20 @@ const supportedGrantTypes = [
const authEndpoint = refWithCallbackOnChange(
grantTypeInfo?.authEndpoint,
(value) => {
auth.value.grantTypeInfo.authEndpoint = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
authEndpoint: value,
}
}
)
const clientID = refWithCallbackOnChange(
grantTypeInfo?.clientID,
(value) => {
auth.value.grantTypeInfo.clientID = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
clientID: value,
}
}
)
Expand All @@ -565,7 +605,10 @@ const supportedGrantTypes = [
return
}
auth.value.grantTypeInfo.clientSecret = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
clientSecret: value,
}
}
)
Expand All @@ -583,7 +626,10 @@ const supportedGrantTypes = [
return
}
auth.value.grantTypeInfo.username = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
username: value,
}
}
)
Expand All @@ -594,7 +640,10 @@ const supportedGrantTypes = [
return
}
auth.value.grantTypeInfo.password = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
password: value,
}
}
)
Expand Down Expand Up @@ -685,21 +734,30 @@ const supportedGrantTypes = [
const authEndpoint = refWithCallbackOnChange(
grantTypeInfo?.authEndpoint,
(value) => {
auth.value.grantTypeInfo.authEndpoint = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
authEndpoint: value,
}
}
)
const clientID = refWithCallbackOnChange(
grantTypeInfo?.clientID,
(value) => {
auth.value.grantTypeInfo.clientID = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
clientID: value,
}
}
)
const scopes = refWithCallbackOnChange(
grantTypeInfo?.scopes ? grantTypeInfo.scopes : undefined,
(value) => {
auth.value.grantTypeInfo.scopes = value
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
scopes: value,
}
}
)
Expand Down Expand Up @@ -786,11 +844,13 @@ const selectedGrantType = computed(() => {
})
const setAccessTokenInActiveTab = (accessToken?: string) => {
if (accessToken) {
if (props.isCollectionProperty && accessToken) {
auth.value.grantTypeInfo = {
...auth.value.grantTypeInfo,
token: accessToken,
}
return
}
if (
Expand Down Expand Up @@ -864,10 +924,22 @@ const generateOAuthToken = async () => {
const localOAuthTempConfig =
persistenceService.getLocalConfig("oauth_temp_config")
const persistedOAuthConfig = localOAuthTempConfig
? JSON.parse(localOAuthTempConfig)
: {}
// Only Grant Types involving redirects require persisting associated information
if (
auth.value.grantTypeInfo.grantType &&
!grantTypesInvolvingRedirect.includes(auth.value.grantTypeInfo.grantType)
) {
return
}
persistenceService.setLocalConfig(
"oauth_temp_config",
JSON.stringify({
...(localOAuthTempConfig ? JSON.parse(localOAuthTempConfig) : {}),
...persistedOAuthConfig,
source: props.source,
context: props.isCollectionProperty
? { type: "collection-properties", metadata: {} }
Expand Down
Expand Up @@ -30,7 +30,7 @@ const AuthCodeOauthFlowParamsSchema = AuthCodeGrantTypeParams.pick({
params.tokenEndpoint.length >= 1 &&
params.clientID.length >= 1 &&
params.clientSecret.length >= 1 &&
(params.scopes === undefined || params.scopes.trim().length >= 1)
(!params.scopes || params.scopes.trim().length >= 1)
)
},
{
Expand Down
Expand Up @@ -25,7 +25,7 @@ const ClientCredentialsFlowParamsSchema = ClientCredentialsGrantTypeParams.pick(
params.authEndpoint.length >= 1 &&
params.clientID.length >= 1 &&
params.clientSecret.length >= 1 &&
(params.scopes === undefined || params.scopes.length >= 1)
(!params.scopes || params.scopes.length >= 1)
)
},
{
Expand Down
Expand Up @@ -27,7 +27,7 @@ const PasswordFlowParamsSchema = PasswordGrantTypeParams.pick({
params.clientSecret.length >= 1 &&
params.username.length >= 1 &&
params.password.length >= 1 &&
(params.scopes === undefined || params.scopes.length >= 1)
(!params.scopes || params.scopes.length >= 1)
)
},
{
Expand Down Expand Up @@ -79,7 +79,7 @@ const initPasswordOauthFlow = async ({

const res = await response

if (E.isLeft(res)) {
if (E.isLeft(res) || res.right.status !== 200) {
toast.error("AUTH_TOKEN_REQUEST_FAILED")
return E.left("AUTH_TOKEN_REQUEST_FAILED" as const)
}
Expand Down

0 comments on commit 7bd4692

Please sign in to comment.