GithubHelp home page GithubHelp logo

sharepoint-nodejs / pnp-auth Goto Github PK

View Code? Open in Web Editor NEW
15.0 2.0 4.0 199 KB

node-sp-auth with pnp/pnpjs (aka PnPjs) integration

License: MIT License

TypeScript 100.00%
sharepoint sharepoint-online sharepoint-2013 sharepoint-2016 pnp pnp-js-core

pnp-auth's Introduction

pnp-auth adds additional authentication options for PnPjs library via implementing custom NodeFetchClient

NPM

npm version Downloads Gitter chat

!Important: as library implements NodeFetchClient and depends on node-sp-auth module, you can use pnp-auth only in nodejs environment

pnp-auth uses node-sp-auth as authentication library, thus making all authentication options from node-sp-auth available for pnp-auth.

Supported versions:

  • SharePoint 2013 and onwards
  • SharePoint Online

For full list of authentication options check out node-sp-auth readme.

How to use

Install

Note on PnPjs v1 usage

If you need support for the previous version of PnPjs, simply install the version of pnp-auth, which supports PnPjs v1:

npm install [email protected]

Install @pnp/sp libraries (they are listed as peer dependencies for pnp-auth, that's why you should install them separately). We need more than just @pnp/sp because it depends on some other @pnp/ packages:

npm install @pnp/logging @pnp/common @pnp/odata @pnp/sp --save

Install pnp-auth

npm install pnp-auth --save

Bootstrap

Before using PnPjs library, you should make it aware of your authentication data. That should be performed at the start of your application. The code is fairly simple:

import { bootstrap } from 'pnp-auth';
import { sp } from '@pnp/sp-commonjs';

bootstrap(sp, authData, siteUrl);
// That's it! Now you can use pnp-sp library:

sp.web.get().then(...);

OR with factory methods:

import { bootstrap } from 'pnp-auth';
import { sp, Web } from '@pnp/sp-commonjs';

bootstrap(sp, authData); 
// That's it! Now you can use pnp-sp library:

let web = Web(siteUrl);
web.get().then(...)

API:

bootstrap(sp, authData, siteUrl)

  • sp - "sp" object obtained from @pnp/sp-commonjs library via import: import { sp } from '@pnp/sp-commonjs';
  • authData - can be a string, AuthConfig object or raw node-sp-auth credentials:
    • string - absolute or relative path to your file with authentication data. File should be generated using node-sp-auth-config CLI. When string is provided, pnp-auth internally creates AuthConfig with below default parameters:
    let authConfig = new AuthConfig({
      configPath: <your path to file>,
      encryptPassword: true,
      saveConfigOnDisk: true
    });
    • AuthConfig - you can provide AuthConfig directly. To learn more checkout node-sp-auth-config repository
    • raw credentials - you can pass any credential options which are supported by node-sp-auth. For more information checkout node-sp-auth repository as well as wiki
  • siteUrl - your SharePoint site url. You have two options when working with SharePoint data. When using siteUrl parameter, you can write a code sp.web.get() etc., in that case your sp.web object will be attached to your siteUrl. If you want to work with different webs, you can use factory method: Web(<url to SharePoint>)

Manual bootstrap

Of course, you can do bootstrap manually, if you want. pnp-auth exports NodeFetchClient which you can use in pnp's setup method:

import NodeFetchClient from 'pnp-auth';
import { sp } from '@pnp/sp-commonjs';

sp.setup({
  sp: {
    fetchClientFactory: () => {
      return new NodeFetchClient(authData, siteUrl);
    }
  }
});

Development

  1. npm install
  2. npm run build - tslint & TS compile

Testing

Library has a few integration tests:

  1. npm install
  2. Rename settings.sample.ts to settings.ts. Update webTitle and subsiteUrl to your real data.
  3. Use node-sp-auth-config to generate credentials inside ./config/private.json file. Site url in credentials should point to site with webTitle from step #2.
  4. Run npm test

pnp-auth's People

Contributors

koltyakov avatar s-kainet avatar spwizard01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pnp-auth's Issues

@pnp v3?

Is there a way to use this with the v3 @pnp libraries?

Compilation error

Hi,

I am getting this error while compiling my project.

Module not found: Error: Can't resolve 'readline' in './node_modules/node-sp-auth-config/node_modules/inquirer/lib/ui/baseUI.js'

I am using Create React app using this https://create-react-app.dev then followed this stackoverflow to add pnpjs to connect to my on prem sharepoint 2013 site
https://sharepoint.stackexchange.com/questions/246264/pnpjs-with-on-prem-sharepoint

It looks like pnp-auth uses node-sp-suth-config as dependecy and node-sp-suth-config as dependecy uses inquirer.js. Hovering on var readline = require('readline');, it takes me to
TypeScript\3.6\node_modules@types\node

Now, I m wondering problem in the version of typescript. Please someone help me to the resolve this issues.

My packages:

"dependencies": {
"@pnp/common": "^1.3.5",
"@pnp/graph": "^1.3.5",
"@pnp/logging": "^1.3.5",
"@pnp/nodejs": "^1.3.5",
"@pnp/odata": "^1.3.5",
"@pnp/sp": "^1.3.5",
"@types/jest": "24.0.18",
"@types/node": "12.7.4",
"@types/react": "16.9.2",
"@types/react-dom": "16.9.0",
"fetch": "^1.1.0",
"office-ui-fabric-react": "^7.31.1",
"pnp-auth": "0.0.4",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-scripts": "3.1.1",
"typescript": "3.6"
},

node version: v8.11.1
npm version: v5.6.0

Thank you !

TypeError: sp.setup is not a function

Hi,

I installed pnp-auth v2.0.0 and @pnp/sp-commonjs v2.11.0 and I'm using them in my project in this way:

const bootstrap = require("pnp-auth").bootstrap;
const taxonomy = import("@pnp/sp-commonjs/taxonomy/index.js");

let url = settings.sharepoint365.url;

bootstrap(taxonomy, {
    username: settings.sharepoint.auth.username, 
    password: settings.sharepoint.auth.password }, 
    url);

But I send my request, I get this error:

C:\my-project\node_modules\pnp-auth\lib\index.js:17
    sp.setup({
       ^

TypeError: sp.setup is not a function
    at bootstrap (C:\my-project\node_modules\pnp-auth\lib\index.js:17:8)
...
..., etc.

Am I doing anything wrong or is this a bug?

Thank you.

Need for bootstrapping @pnp/sp-taxonomy?

This package is awesome. Thanks for your work on this, it made it so easy to drop @pnp/sp into node.js. I have a need to do the same thing with @pnp/sp-taxonomy. I'm thinking about forking and adding that capability, but am wondering if anyone else would see the benefit.

Error with sp.createIsolated()

Hello,
I'm using pnp-auth a lot :) thank you for providing this lib

I'm trying to use isolated sp objects with pnp-auth but I get the following error :

TypeError: safe_global_js_1.safeGlobal.fetch is not a function
(node:13276) UnhandledPromiseRejectionWarning: TypeError: safe_global_js_1.safeGlobal.fetch is not a function
at FetchClient.fetch (C:\Projects\sptoolkit-projects\packages\common\net.ts:66:27)
at SPHttpClient.retry (C:\Projects\sptoolkit-projects\packages\sp\sphttpclient.ts:122:24)
at C:\Projects\sptoolkit-projects\packages\sp\sphttpclient.ts:144:19
at new Promise ()
at SPHttpClient.fetchRaw (C:\Projects\sptoolkit-projects\packages\sp\sphttpclient.ts:142:16)
at SPHttpClient. (C:\Projects\sptoolkit-projects\packages\sp\sphttpclient.ts:82:21)

packages used :

 "@pnp/nodejs-commonjs": "^2.6.0",
  "@pnp/sp-commonjs": "^2.6.0",
   "pnp-auth": "^2.0.0",

Code that produces the error:

   import { bootstrap, } from 'pnp-auth';
   import { sp } from '@pnp/sp-commonjs';
   import { AuthConfig, } from 'node-sp-auth-config'
   
   const isolatedSP = await sp.createIsolated()
   const authConfig = new AuthConfig({
		authOptions: thecredsobject
	});
   bootstrap(isolatedSP, authConfig)

The same code works with the standard sp object (from the import)

     .....
      bootstrap(sp, authConfig)

I hope you can point me to my error or fix the problem ?

thanks again
gr

Module not Working with new NodeJS Version 13.2 on TypeScript

Hello Guys,
i recently came up with the Problem that i couldnt compile my project.
The following error came up
Bildschirmfoto 2020-04-29 um 11 11 22

So i looked into your code and saw that your tsconfig.json was compiling to "es6".
To remove the Error i changed "es6" to "es5" and i had to add the option "lib": ["es2015"]

The new tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "declaration": true,
    "moduleResolution": "node",
    "noImplicitAny": true,
    "removeComments": true,
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noStrictGenericChecks": true,
    "skipLibCheck": true,
    "outDir": "./lib",
    "lib": ["es2015"]
  },
  "include": [
    "src/**/*.ts"
  ]
}

I also added some dependencies to the package.json but i guess it will work without them.
Just if it matters here are the libraries i came up with:

"dependencies": {
    "@pnp/common": "^1.3.11", (new)
    "@pnp/logging": "^2.0.4", (new)
    "@pnp/odata": "^2.0.4", (new)
    "@pnp/sp": "^2.0.4", (new)
    "@pnp/sp-commonjs": "^2.0.4", (new)
    "node-fetch": "^2.2.0",
    "node-sp-auth": "^2.5.5",
    "node-sp-auth-config": "^2.5.6"
  }

Im not a pro at coding but i hope the changes help you.
Wish you the best, nice project!

Compilation Errors - @pnp/sp @pnp/pnpjs ^2.0.3 and pnp-auth

Hello,

I have installed

@pnp/sp - 2.0.3
@pnp/pnpjs - 2.0.3
@pnp/common - 2.03
@pnp/logging - 2.0.3
@pnp/nodejs-commonjs - 2.0.3 -- added but not used
@pnp/sp-common - 2.0.3 -- added but not used
@pnp/odata - 2.0.3
pnp-auth - 0.0.4
sp-pnp-node: 2.2.0

and trying to do a simple

import {sp} from '@pnp/sp
bootstap(sp, ..private.json, siteURL)

I am getting the below compilation errors

  1. FetchOption
    node-modules/pnp-auth/lib/NodeFetchClient.d.ts:1:26 - error TS274 : Module ..../@pnp/common has no exported member 'FetchOption' Did you mean IFetchOption

2.HttpClientImpl
node-modules/pnp-auth/lib/NodeFetchClient.d.ts:1:26 - error TS274 : Module ..../@pnp/common has no exported member 'HttpClientImpl' Did you mean IHttpClientImpl

Any suggestions on where I am going wrong ..

Thanks

Provide custom fetch

I have a misbehaving SP with frequent timeouts that I can't do anything about. I would like to make it so GET requests have a short timeout (default seems to be 15 minutes) and are retried 3 times when they time out.

It seems that I have to monkey-patch node-fetch's fetch to do so, but it would be nice to provide a custom fetch function instead.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.