api
This commit is contained in:
@ -0,0 +1,50 @@
|
||||
// from https://github.com/pedroetb/node-oauth2-server-example
|
||||
|
||||
let Http = require("http")
|
||||
let path = require("path")
|
||||
let express = require("express")
|
||||
let bodyParser = require("body-parser")
|
||||
let oauthserver = require("oauth2-server")
|
||||
let cors = require("cors")
|
||||
|
||||
let app = express()
|
||||
|
||||
app.use(cors())
|
||||
|
||||
app.use(bodyParser.urlencoded({ extended: true }))
|
||||
|
||||
app.use(bodyParser.json())
|
||||
|
||||
app.oauth = oauthserver({
|
||||
model: require("./model.js"),
|
||||
grants: ["password", "client_credentials", "implicit"],
|
||||
debug: true
|
||||
})
|
||||
|
||||
app.all("/oauth/token", app.oauth.grant())
|
||||
|
||||
app.get("/swagger.yaml", function (req, res) {
|
||||
res.sendFile(path.join(__dirname, "swagger.yaml"))
|
||||
})
|
||||
|
||||
app.get("*", app.oauth.authorise(), function (req, res) {
|
||||
res.send("Secret secrets are no fun, secret secrets hurt someone.")
|
||||
})
|
||||
|
||||
app.use(app.oauth.errorHandler())
|
||||
|
||||
function startServer() {
|
||||
let httpServer = Http.createServer(app)
|
||||
httpServer.listen("3231")
|
||||
|
||||
return function stopServer() {
|
||||
httpServer.close()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = startServer
|
||||
|
||||
if (require.main === module) {
|
||||
// for debugging
|
||||
startServer()
|
||||
}
|
@ -0,0 +1,141 @@
|
||||
// from https://github.com/pedroetb/node-oauth2-server-example
|
||||
|
||||
let config = {
|
||||
clients: [{
|
||||
clientId: "application",
|
||||
clientSecret: "secret"
|
||||
}],
|
||||
confidentialClients: [{
|
||||
clientId: "confidentialApplication",
|
||||
clientSecret: "topSecret"
|
||||
}],
|
||||
tokens: [],
|
||||
users: [{
|
||||
id: "123",
|
||||
username: "swagger",
|
||||
password: "password"
|
||||
}]
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump the memory storage content (for debug).
|
||||
*/
|
||||
|
||||
let dump = function () {
|
||||
|
||||
console.log("clients", config.clients)
|
||||
console.log("confidentialClients", config.confidentialClients)
|
||||
console.log("tokens", config.tokens)
|
||||
console.log("users", config.users)
|
||||
}
|
||||
|
||||
/*
|
||||
* Methods used by all grant types.
|
||||
*/
|
||||
|
||||
let getAccessToken = function (bearerToken, callback) {
|
||||
|
||||
let tokens = config.tokens.filter(function (token) {
|
||||
|
||||
return token.accessToken === bearerToken
|
||||
})
|
||||
|
||||
return callback(false, tokens[0])
|
||||
}
|
||||
|
||||
let getClient = function (clientId, clientSecret, callback) {
|
||||
|
||||
let clients = config.clients.filter(function (client) {
|
||||
|
||||
return client.clientId === clientId && client.clientSecret === clientSecret
|
||||
})
|
||||
|
||||
let confidentialClients = config.confidentialClients.filter(function (client) {
|
||||
|
||||
return client.clientId === clientId && client.clientSecret === clientSecret
|
||||
})
|
||||
|
||||
callback(false, clients[0] || confidentialClients[0])
|
||||
}
|
||||
|
||||
let grantTypeAllowed = function (clientId, grantType, callback) {
|
||||
|
||||
let clientsSource,
|
||||
clients = []
|
||||
|
||||
if (grantType === "password") {
|
||||
clientsSource = config.clients
|
||||
} else if (grantType === "client_credentials") {
|
||||
clientsSource = config.confidentialClients
|
||||
}
|
||||
|
||||
if (clientsSource) {
|
||||
clients = clientsSource.filter(function (client) {
|
||||
|
||||
return client.clientId === clientId
|
||||
})
|
||||
}
|
||||
|
||||
callback(false, clients.length)
|
||||
}
|
||||
|
||||
let saveAccessToken = function (accessToken, clientId, expires, user, callback) {
|
||||
|
||||
config.tokens.push({
|
||||
accessToken: accessToken,
|
||||
expires: expires,
|
||||
clientId: clientId,
|
||||
user: user
|
||||
})
|
||||
|
||||
callback(false)
|
||||
}
|
||||
|
||||
/*
|
||||
* Method used only by password grant type.
|
||||
*/
|
||||
|
||||
let getUser = function (username, password, callback) {
|
||||
|
||||
let users = config.users.filter(function (user) {
|
||||
|
||||
return user.username === username && user.password === password
|
||||
})
|
||||
|
||||
callback(false, users[0])
|
||||
}
|
||||
|
||||
/*
|
||||
* Method used only by client_credentials grant type.
|
||||
*/
|
||||
|
||||
let getUserFromClient = function (clientId, clientSecret, callback) {
|
||||
|
||||
let clients = config.confidentialClients.filter(function (client) {
|
||||
|
||||
return client.clientId === clientId && client.clientSecret === clientSecret
|
||||
})
|
||||
|
||||
let user
|
||||
|
||||
if (clients.length) {
|
||||
user = {
|
||||
username: clientId
|
||||
}
|
||||
}
|
||||
|
||||
callback(false, user)
|
||||
}
|
||||
|
||||
/**
|
||||
* Export model definition object.
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
getAccessToken: getAccessToken,
|
||||
getClient: getClient,
|
||||
grantTypeAllowed: grantTypeAllowed,
|
||||
saveAccessToken: saveAccessToken,
|
||||
getUser: getUser,
|
||||
getUserFromClient: getUserFromClient
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
swagger: "2.0"
|
||||
host: localhost:3231
|
||||
paths:
|
||||
/password:
|
||||
get:
|
||||
summary: OAuth2 Password
|
||||
security:
|
||||
- oauthPassword: []
|
||||
responses:
|
||||
200:
|
||||
description: OK
|
||||
schema:
|
||||
type: string
|
||||
/application:
|
||||
get:
|
||||
summary: OAuth2 Application
|
||||
security:
|
||||
- oauthApplication: []
|
||||
responses:
|
||||
200:
|
||||
description: OK
|
||||
schema:
|
||||
type: string
|
||||
securityDefinitions:
|
||||
oauthPassword:
|
||||
type: oauth2
|
||||
flow: password
|
||||
tokenUrl: /oauth/token
|
||||
oauthApplication:
|
||||
type: oauth2
|
||||
flow: application
|
||||
tokenUrl: /oauth/token
|
||||
oauthImplicit:
|
||||
type: oauth2
|
||||
flow: implicit
|
||||
authorizationUrl: /oauth/token
|
Reference in New Issue
Block a user