-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-merge for PR #721 via VersionBot
Inline the entire resin-cli-auth module
- Loading branch information
Showing
16 changed files
with
636 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
### | ||
Copyright 2016 Resin.io | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
### | ||
|
||
###* | ||
# @module auth | ||
### | ||
|
||
open = require('open') | ||
resin = require('resin-sdk-preconfigured') | ||
server = require('./server') | ||
utils = require('./utils') | ||
|
||
###* | ||
# @summary Login to the Resin CLI using the web dashboard | ||
# @function | ||
# @public | ||
# | ||
# @description | ||
# This function opens the user's default browser and points it | ||
# to the Resin.io dashboard where the session token exchange will | ||
# take place. | ||
# | ||
# Once the the token is retrieved, it's automatically persisted. | ||
# | ||
# @fulfil {String} - session token | ||
# @returns {Promise} | ||
# | ||
# @example | ||
# auth.login().then (sessionToken) -> | ||
# console.log('I\'m logged in!') | ||
# console.log("My session token is: #{sessionToken}") | ||
### | ||
exports.login = -> | ||
options = | ||
port: 8989 | ||
path: '/auth' | ||
|
||
# Needs to be 127.0.0.1 not localhost, because the ip only is whitelisted | ||
# from mixed content warnings (as the target of a form in the result page) | ||
callbackUrl = "http://127.0.0.1:#{options.port}#{options.path}" | ||
return utils.getDashboardLoginURL(callbackUrl).then (loginUrl) -> | ||
|
||
# Leave a bit of time for the | ||
# server to get up and runing | ||
setTimeout -> | ||
open(loginUrl) | ||
, 1000 | ||
|
||
return server.awaitForToken(options) | ||
.tap(resin.auth.loginWithToken) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="x-ua-compatible" content="ie=edge"> | ||
<title>Resin CLI - Error</title> | ||
<meta name="description" content=""> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="stylesheet" type="text/css" href="./static/style.css" inline> | ||
</head> | ||
<body> | ||
<div class="center"> | ||
<img class="icon" src="./static/images/sad.png" inline> | ||
<h1>Something went wrong</h1> | ||
<p>You couldn't login to the Resin CLI for some reason</p> | ||
<br> | ||
<br> | ||
<a href="https://forums.resin.io/" class="button danger">Get help in our forums</a> | ||
</div> | ||
</body> | ||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
html, | ||
body { | ||
height: 100%; | ||
} | ||
|
||
body { | ||
text-align: center; | ||
background-color: #fff; | ||
color: rgb(24, 24, 24); | ||
font-family: Helvetica Neue, Helvetica, Arial, sans-serif; | ||
position: relative; | ||
} | ||
|
||
.center { | ||
position: absolute; | ||
top: 0; | ||
bottom: 0; | ||
left: 0; | ||
right: 0; | ||
margin: auto; | ||
width: 50%; | ||
height: 50%; | ||
} | ||
|
||
.icon { | ||
display: block; | ||
width: 40px; | ||
height: 45px; | ||
margin: 0 auto; | ||
margin-bottom: 15px; | ||
} | ||
|
||
h1 { | ||
font-size: 3rem; | ||
margin: 0; | ||
margin-bottom: 12px; | ||
} | ||
|
||
p { | ||
color: rgb(99, 99, 99); | ||
font-size: 1.1rem; | ||
margin: 0; | ||
margin-bottom: 15px; | ||
} | ||
|
||
a.button { | ||
padding: 15px 25px; | ||
border-radius: 5px; | ||
text-decoration: none; | ||
} | ||
|
||
a.button.danger { | ||
background-color: rgb(235, 110, 111); | ||
color: #fff; | ||
} | ||
|
||
a.button.normal { | ||
background-color: rgb(252, 191, 44); | ||
color: #fff; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="x-ua-compatible" content="ie=edge"> | ||
<title>Resin CLI - Success</title> | ||
<meta name="description" content=""> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="stylesheet" type="text/css" href="./static/style.css" inline> | ||
</head> | ||
<body> | ||
<div class="center"> | ||
<img class="icon" src="./static/images/happy.png" inline> | ||
<h1>Success!</h1> | ||
<p>You successfully logged in the Resin CLI</p> | ||
<br> | ||
<br> | ||
<a href="<%= dashboardUrl %>" class="button normal">Go to the dashboard</a> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
### | ||
Copyright 2016 Resin.io | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
### | ||
|
||
express = require('express') | ||
path = require('path') | ||
bodyParser = require('body-parser') | ||
Promise = require('bluebird') | ||
resin = require('resin-sdk-preconfigured') | ||
utils = require('./utils') | ||
|
||
createServer = ({ port, isDev } = {}) -> | ||
app = express() | ||
app.use bodyParser.urlencoded | ||
extended: true | ||
|
||
app.set('view engine', 'ejs') | ||
app.set('views', path.join(__dirname, 'pages')) | ||
|
||
if isDev | ||
app.use(express.static(path.join(__dirname, 'pages', 'static'))) | ||
|
||
server = app.listen(port) | ||
|
||
return { app, server } | ||
|
||
###* | ||
# @summary Await for token | ||
# @function | ||
# @protected | ||
# | ||
# @param {Object} options - options | ||
# @param {String} options.path - callback path | ||
# @param {Number} options.port - http port | ||
# | ||
# @example | ||
# server.awaitForToken | ||
# path: '/auth' | ||
# port: 9001 | ||
# .then (token) -> | ||
# console.log(token) | ||
### | ||
exports.awaitForToken = (options) -> | ||
{ app, server } = createServer(port: options.port) | ||
|
||
return new Promise (resolve, reject) -> | ||
closeServer = (errorMessage, successPayload) -> | ||
server.close -> | ||
if errorMessage | ||
reject(new Error(errorMessage)) | ||
return | ||
|
||
resolve(successPayload) | ||
|
||
renderAndDone = ({ request, response, viewName, errorMessage, statusCode, token }) -> | ||
return getContext(viewName) | ||
.then (context) -> | ||
response.status(statusCode || 200).render(viewName, context) | ||
request.connection.destroy() | ||
closeServer(errorMessage, token) | ||
|
||
app.post options.path, (request, response) -> | ||
token = request.body.token?.trim() | ||
|
||
Promise.try -> | ||
if not token | ||
throw new Error('No token') | ||
return utils.isTokenValid(token) | ||
.tap (isValid) -> | ||
if not isValid | ||
throw new Error('Invalid token') | ||
.then -> | ||
renderAndDone({ request, response, viewName: 'success', token }) | ||
.catch (error) -> | ||
renderAndDone({ | ||
request, response, viewName: 'error', | ||
statusCode: 401, errorMessage: error.message | ||
}) | ||
|
||
app.use (request, response) -> | ||
response.status(404).send('Not found') | ||
closeServer('Unknown path or verb') | ||
|
||
exports.getContext = getContext = (viewName) -> | ||
if viewName is 'success' | ||
return Promise.props | ||
dashboardUrl: resin.settings.get('dashboardUrl') | ||
|
||
return Promise.resolve({}) |
Oops, something went wrong.