Skip to content

SharePoint Online Authentication Unsuggestive Error Message #127

@liviu-gheorghe

Description

@liviu-gheorghe

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions