Giter Site home page Giter Site logo

spo-remote-auth's Introduction

CircleCI license

@beyond-sharepoint/spo-remote-auth

Provides a generic remote authentication implementation against SharePoint online.

To use, simply import the module and supply the tenant url and credentials.

const spo = require("@beyond-sharepoint/spo-remote-auth");

spo.authenticate("mytenantname.sharepoint.com", "[email protected]", "mypassword")
    .then(function(ctx) {
        console.log("Success!!");
    }, function() {
        console.log("something went wrong.");
    });

The object that is returned when authentication succeeds contains the following properties:

contextInfo

Object that contains the context info returned by .sharepoint.com/_api/contextinfo

ensureContextInfo

Helper function that renews the context info if it has expired.

request

A request instance with the defaults set to what needs to be passed to SPO.

Use this to make further authenticated calls with SharePoint online.

For instance, to upload a file to a document library:

const spo = require("@beyond-sharepoint/spo-remote-auth");
const URI = require("urijs");

spo.authenticate("mytenantname.sharepoint.com", "[email protected]", "mypassword")
    .then(function(ctx) {
        //upload a file to 'documents' library.

        let docLibUrl = "documents";
        let fileName = "test1234.txt";

        ctx.request({
            method: "POST",
            url: URI.joinPaths("/_api/web/", `GetFolderByServerRelativeUrl('${URI.encode(docLibUrl)}')/`, "files/", `add(url='${URI.encode(fileName)}',overwrite=true)`).href(),
            body: "Hello, world!"
        });
    });

Other packages on Beyond SharePoint provide concerted functionality.

Unit Testing

spo-remote-auth uses mocha/chai based unit tests to ensure quality.

By default, the unit tests use mock service responses via nock.

just run npm test at the cli to run the tests:

$ npm test

    ✓ should contain an authenticate method
    ✓ should fail with invalid user
    ✓ should fail with invalid password
    ✓ should authenticate and contain a context info that expires in the future.

Unit Test Options

--live

To test against the actual SharePoint online services instead of the mocks, use the --live option.

ex:

$ npm test -- --live

    ✓ should contain an authenticate method
    ✓ should fail with invalid user
    ✓ should fail with invalid password
    ✓ should authenticate and contain a context info that expires in the future.

--settings

You'll quickly find that you'll need to supply your own credentials and tenant name in order to test live, to do so, you can modify the values in /test/fixtures/settings-test.json.

However, a better way is to use the --settings option to specify the name of a settings file that you provide. Note that this file is relative to the /test/fixtures folder.

$ npm test -- --settings settings-prod.json --live

settings-prod.json is included in .gitignore by default.

--record

To aid in debugging, the --record option records all interaction with SPO and places it in /test/tmp/nock-test.json. It is expected that the live option is specified.

$ npm test --  --record --live

--recordOutput

To override the default record file name, use --recordOutput

$ npm test --  --record --live --recordOutput nock-ensureContext.json

spo-remote-auth's People

Contributors

oceanswave avatar

Watchers

lqdchrm avatar James Cloos avatar

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.