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

SharePoint Online Authentication Unsuggestive Error Message #127

Open
liviu-gheorghe opened this issue Aug 11, 2023 · 0 comments
Open

SharePoint Online Authentication Unsuggestive Error Message #127

liviu-gheorghe opened this issue Aug 11, 2023 · 0 comments

Comments

@liviu-gheorghe
Copy link

liviu-gheorghe commented Aug 11, 2023

I'm using node-sp-auth ^3.0.7 to establish a connection to SharePoint online using the following code:

    import * as spAuth from 'node-sp-auth';

    spAuth.getAuth('my-url', {
          username: 'my-username',
          password: 'my-password',
          online: true
      })
      .then((options) => {
              console.log("Successfully authenticated in SPO")
              // Do stuff...
      }).catch((error) => {
          console.error('Error connecting to SPO: ' + error.message)
      })

Occasionally, I get the following error message from the library: Error connecting to SPO: Cannot read properties of undefined (reading 'length')

I think this error message is unsuggestive / misleading, as I cannot figure out why the authentication failed.

Browsing through the source code, I found the following snippet of code that could be the source of the problem:

src/auth/resolvers/OnlineUserCredentials.ts

  public getAuth(): Promise<IAuthResponse> {
    const parsedUrl: url.Url = url.parse(this._siteUrl);
    const host: string = parsedUrl.host;
    const cacheKey = `${host}@${this._authOptions.username}@${this._authOptions.password}`;
    const cachedCookie: string = OnlineUserCredentials.CookieCache.get<string>(cacheKey);

    if (cachedCookie) {
      return Promise.resolve({
        headers: {
          'Cookie': cachedCookie
        }
      });
    }

    return this.getSecurityToken()
      .then(xmlResponse => {
        return this.postToken(xmlResponse);
      })
      .then(data => {
        const response = data[1];
        const diffSeconds: number = data[0];
        let fedAuth: string;
        let rtFa: string;

        for (let i = 0; i < response.headers['set-cookie'].length; i++) {
          const headerCookie: string = response.headers['set-cookie'][i];
          if (headerCookie.indexOf(consts.FedAuth) !== -1) {
            fedAuth = cookie.parse(headerCookie)[consts.FedAuth];
          }
          if (headerCookie.indexOf(consts.RtFa) !== -1) {
            rtFa = cookie.parse(headerCookie)[consts.RtFa];
          }
        }

        const authCookie: string = 'FedAuth=' + fedAuth + '; rtFa=' + rtFa;

        OnlineUserCredentials.CookieCache.set(cacheKey, authCookie, diffSeconds);

        return {
          headers: {
            'Cookie': authCookie
          }
        };
      });
  }

Could response.headers['set-cookie'] be undefined and produce the Cannot read properties of undefined (reading 'length') error message?

I think the message of the error thrown by spAuth.getAuth should be something related to the error received from the upstream SPO server in order to be able to see what gone wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant