Giter Club home page Giter Club logo

okta-node-rest-api-example's Introduction

Simple REST API with Node and OAuth 2.0

This example app shows how to create a REST API in Node and secure it with OAuth 2.0 Client Credentials using Okta. This also has an example client written as a CLI that can authenticate with Okta and use the REST API.

Getting Started

Install Dependencies

After cloning the repository, simply run npm install to install the dependencies.

Save Environment Variables

If you don't have one already, sign up for a free Okta Developer account. Log in to your developer console to get the following information.

Create a file named .env that has the following variables, all obtained from your Okta developer console:

  • ISSUER

    Log in to your developer console and navigate to API > Authorization Servers. Copy the Issuer URI for the default server.

  • SCOPE

    Click on the word default to get details about the authorization server. Go to the Scopes tab and click the Add Scope button. Give it a name and optionally a description. The example app is for a parts manager, so for example you could name it parts_manager.

  • CLIENT_ID

    Navigate to Applications, then click the Add Application button. Select Service, then click Next. Choose a name then click Done. The Client ID is shown on the next page.

  • CLIENT_SECRET

    The Client Secret is on the same page as the Client ID

When you're done, your .env file should look something like this:

ISSUER=https://dev-123456.oktapreview.com/oauth2/default
SCOPE=parts_manager
CLIENT_ID=0123456789abcdefghij
CLIENT_SECRET=0123456789abcdefghijklmnopqrstuvwxyz0123

Run the Server

To run the server, run npm start from the terminal.

Run the Client

To make secure API requests, you'll need to use the client located at client.js.

USAGE

node client url [method] [jsonString]

  • url (required): the path to your server along with the endpoint (e.g. http://localhost:3000/parts)
  • method (optional): the HTTP verb for the REST call (e.g. delete or post). Defaults to get
  • jsonString (optional): stringified JSON data for use in put or post calls (e.g. '{"partNumber":"asdf-1234"}')

EXAMPLES

  • node client http://localhost:3000/parts: get a list of all parts
  • node client http://localhost:3000/parts post '{"partNumber":"asdf-1234"}': creates a new part with part number asdf-1234
  • node client http://localhost:3000/parts/1: gets details about the part with and id of 1
  • node client http://localhost:3000/parts/7 put '{"name":"A single dairy farm"}': updates the name field of the part with an id of 7
  • node client http://localhost:3000/parts/11 delete: deletes the part with an id of 11

License

Apache 2.0, see LICENSE.

okta-node-rest-api-example's People

Contributors

dependabot[bot] avatar redbmk avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.