-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
378 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,18 @@ | ||
{ | ||
"port": "80", | ||
"secret": "secret", | ||
"expiresIn": "8h", | ||
"github": { | ||
"clienteId": "93c87ec9bfbea4151180", | ||
"clienteSecret": "8e1d50fb000071ebd47a7c970fce9365c831f287", | ||
"callbackURL": "https://frontendbreventosapi-eventosapi.wedeploy.io/auth/github/callback" | ||
"client_id": "7ce460021a69ec7f56b7", | ||
"client_secret": "d58c251c95c9ce057c9532aacb0fb2f33e0412f8", | ||
"redirect_uri": "http://localhost:3000/auth/callback", | ||
"grant_type": "grant_type" | ||
}, | ||
"serviceAccountAdminPath": "./firebase-admin-sdk.json", | ||
"databaseURL": "https://frontend-br-eventos.firebaseio.com", | ||
"mongo": { | ||
"host": "ds227555.mlab.com", | ||
"port": 27555, | ||
"database": "eventos-api", | ||
"username": "admin", | ||
"password": "javascript0" | ||
}, | ||
"firebase": { | ||
"apiKey": "AIzaSyDClNYE54DCbk5y91sf-_MCYLIfkto6zwk", | ||
"authDomain": "frontend-br-eventos.firebaseapp.com", | ||
"databaseURL": "https://frontend-br-eventos.firebaseio.com", | ||
"projectId": "frontend-br-eventos", | ||
"storageBucket": "frontend-br-eventos.appspot.com", | ||
"messagingSenderId": "477638030798" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,18 +1,8 @@ | ||
{ | ||
"port": "3000", | ||
"serviceAccountAdminPath": "./firebase-admin-sdk-dev.json", | ||
"databaseURL": "https://frontend-br-eventos-dev.firebaseio.com/", | ||
"firebase": { | ||
"apiKey": "AIzaSyAH12r5RbxzHZA30ZLc-nAMqlEXLmYxzVE", | ||
"authDomain": "frontend-br-eventos-dev.firebaseapp.com", | ||
"databaseURL": "https://frontend-br-eventos-dev.firebaseio.com", | ||
"projectId": "frontend-br-eventos-dev", | ||
"storageBucket": "", | ||
"messagingSenderId": "90026726723" | ||
}, | ||
"github": { | ||
"clienteId": "7ce460021a69ec7f56b7", | ||
"clienteSecret": "d58c251c95c9ce057c9532aacb0fb2f33e0412f8", | ||
"callbackURL": "http://localhost:3000/auth/github/callback" | ||
} | ||
} | ||
{ | ||
"port": "3000", | ||
"github": { | ||
"clienteId": "7ce460021a69ec7f56b7", | ||
"clienteSecret": "d58c251c95c9ce057c9532aacb0fb2f33e0412f8", | ||
"callbackURL": "http://localhost:3000/auth/github/callback" | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Binary file removed
BIN
-2.31 KB
src/admin-firebase/frontend-br-eventos-firebase-adminsdk-cwau8-feba252aa4.json.enc
Binary file not shown.
This file was deleted.
Oops, something went wrong.
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,68 @@ | ||
import jwt from 'jsonwebtoken' | ||
import config from 'config' | ||
import Administrator from '../admin/model' | ||
|
||
export class Authentication { | ||
constructor () { | ||
this.config = config | ||
this.jwt = jwt | ||
} | ||
|
||
checkAuth (request) { | ||
return this._validToken(request) | ||
} | ||
|
||
checkAdmin (request) { | ||
return this._validToken(request) | ||
.then(({ email }) => { | ||
return Administrator | ||
.findOne({ email }) | ||
.exec() | ||
}).then((adm) => { | ||
if (!adm) { | ||
return Promise.reject(new Error('É necessário ser adm')) | ||
} | ||
return adm | ||
}) | ||
} | ||
|
||
_validToken (request) { | ||
return new Promise((resolve, reject) => { | ||
const jwtToken = this._getToken(request) | ||
if (!jwtToken) { | ||
reject(new Error('É necessário informar um token')) | ||
} | ||
const userInfo = this.jwt.verify(jwtToken, this.config.secret) | ||
|
||
if (!userInfo) { | ||
reject(new Error('É necessário estar logado')) | ||
} | ||
|
||
resolve(userInfo) | ||
}) | ||
} | ||
|
||
createToken (email) { | ||
return { token: this.jwt.sign({ email }, this.config.secret, { expiresIn: this.config.expiresIn }) } | ||
} | ||
|
||
_getToken (request) { | ||
const query = request.query | ||
const headers = request.headers | ||
|
||
let token = null | ||
|
||
const bearerHeader = headers.authorization | ||
if (bearerHeader && bearerHeader.indexOf(' ') > -1) { | ||
token = bearerHeader.split(' ')[1] | ||
} | ||
|
||
if (!token && query.token) { | ||
token = query.token | ||
} | ||
|
||
return token | ||
} | ||
} | ||
|
||
export default new Authentication() |
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,31 @@ | ||
import axios from 'axios' | ||
import config from 'config' | ||
import { success, error } from './../../constants' | ||
import Authentication from './Authentication' | ||
|
||
export const auth = (request, response) => { | ||
const { code } = request.body | ||
|
||
let data = Object.assign({ code }, config.github) | ||
|
||
axios | ||
.post('https://github.com/login/oauth/access_token', data, params) | ||
.then(getUserInfo) | ||
.then(success(response)) | ||
.catch(error(response)) | ||
} | ||
|
||
const params = { | ||
headers: { | ||
'Content-Type': 'application/json', 'Accept': 'application/json' | ||
} | ||
} | ||
|
||
const generateJWT = ({ email }) => Authentication.createToken(email) | ||
|
||
const getUserInfo = ({ data }) => { | ||
return axios | ||
.get(`https://api.github.com/user?access_token=${data.access_token}`, params) | ||
.then((resp) => resp.data) | ||
.then(generateJWT) | ||
} |
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,14 @@ | ||
import { Router } from 'express' | ||
|
||
import { auth } from './controller' | ||
|
||
const router = () => { | ||
console.info('Init Auth module') | ||
const route = Router() | ||
|
||
route.post('/', auth) | ||
|
||
return route | ||
} | ||
|
||
export default router |
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
Oops, something went wrong.