Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impersonation fixes #841

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions packages/graphql-client/src/graphql-client.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { TransportInterface, AccountsClient } from '@accounts/client';
import { CreateUser, LoginResult, ImpersonationResult, User } from '@accounts/types';
import { AccountsClient, TransportInterface } from '@accounts/client';
import { CreateUser, ImpersonationResult, LoginResult, User } from '@accounts/types';
import gql from 'graphql-tag';
import { authenticateWithServiceMutation } from './graphql/authenticate-with-service.mutation';
import { changePasswordMutation } from './graphql/change-password.mutation';
import { createUserMutation } from './graphql/create-user.mutation';
import { getTwoFactorSecretQuery } from './graphql/get-two-factor-secret.query';
import { getUserQuery } from './graphql/get-user.query';
import { impersonateMutation } from './graphql/impersonate.mutation';
import { loginWithServiceMutation } from './graphql/login-with-service.mutation';
import { authenticateWithServiceMutation } from './graphql/authenticate-with-service.mutation';
import { logoutMutation } from './graphql/logout.mutation';
import { refreshTokensMutation } from './graphql/refresh-tokens.mutation';
import { verifyEmailMutation } from './graphql/verify-email.mutation';
import { resetPasswordMutation } from './graphql/reset-password.mutation';
import { sendResetPasswordEmailMutation } from './graphql/send-reset-password-email.mutation';
import { sendVerificationEmailMutation } from './graphql/send-verification-email.mutation';
import { resetPasswordMutation } from './graphql/reset-password.mutation';
import { changePasswordMutation } from './graphql/change-password.mutation';
import { twoFactorSetMutation } from './graphql/two-factor-set.mutation';
import { getTwoFactorSecretQuery } from './graphql/get-two-factor-secret.query';
import { twoFactorUnsetMutation } from './graphql/two-factor-unset.mutation';
import { impersonateMutation } from './graphql/impersonate.mutation';
import { getUserQuery } from './graphql/get-user.query';
import gql from 'graphql-tag';
import { verifyEmailMutation } from './graphql/verify-email.mutation';

export interface IAuthenticateParams {
[key: string]: string | object;
Expand Down Expand Up @@ -154,19 +154,20 @@ export default class GraphQLClient implements TransportInterface {
public async impersonate(
token: string,
impersonated: {
userId?: string;
username?: string;
email?: string;
/* These aren't implemented in graphql-api, comment them out for now to avoid confusion */
// userId?: string;
// email?: string;
}
): Promise<ImpersonationResult> {
return this.mutate(impersonateMutation, 'impersonate', {
return this.mutate(impersonateMutation(this.options.userFieldsFragment), 'impersonate', {
accessToken: token,
username: impersonated.username,
});
}

private async mutate(mutation: any, resultField: any, variables: any = {}) {
// If we are executiong a refresh token mutation do not call refress session again
// If we are executing a refresh token mutation do not call refresh session again
// otherwise it will end up in an infinite loop
const tokens =
mutation === refreshTokensMutation
Expand Down
8 changes: 3 additions & 5 deletions packages/graphql-client/src/graphql/impersonate.mutation.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import gql from 'graphql-tag';

export const impersonateMutation = gql`
export const impersonateMutation = (userFieldsFragment: any) => gql`
${userFieldsFragment}
mutation impersonate($accessToken: String!, $username: String!) {
impersonate(accessToken: $accessToken, username: $username) {
authorized
tokens {
refreshToken
accessToken
}
# // TODO: Extract user into a fragment
user {
id
email
username
...userFields
}
}
}
Expand Down