forked from RocketChat/Rocket.Chat.PWA
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Oauth #19
Merged
Merged
Oauth #19
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
15bc2cb
fixed side-nav size - like in rocketchat
eb2e57e
removed empty line
b74db18
Merge branch 'PWA' into oauth
858adec
oauth half way there :)
43159db
started working
99fdb0d
refactored code on server
30bca3f
minor change
c3ee554
refactored OAuth
db1fd16
changed accountsserver back to singleton - much easier to work with -…
e71bf9c
fixed authorization bug in client
9528582
removed unused dep
1a40de8
add jsaccounts authentication
56cf649
Merge branch 'PWA' into oauth
75177ea
added find user by oauth id in db to oauth-service
2a1dec9
replaced console.log with console.error
d984359
added console.error to login page
2679520
added oauth id to user profile
b2e4e86
minor fix
e06d28a
fixed otimistic ui date
2f628c7
fixed multiple apollo mutation result callbacks running
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"port": 4200 | ||
} |
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,24 @@ | ||
import { MongoClient } from 'mongodb'; | ||
|
||
let mongoDB; | ||
|
||
const MONGO_URL = 'mongodb://localhost:27017/rocketchatMock'; | ||
|
||
const initMongoClient = async () => { | ||
try { | ||
mongoDB = await MongoClient.connect(MONGO_URL); | ||
} | ||
catch (e) { | ||
console.error('cannot connect to db', e); | ||
} | ||
return mongoDB; | ||
}; | ||
|
||
|
||
export const getMongoClient = async () => { | ||
if (!mongoDB) { | ||
await initMongoClient(); | ||
} | ||
return mongoDB; | ||
}; | ||
|
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,22 @@ | ||
export const GRANT_PATH = '/auth'; | ||
|
||
export const grantConfig = { | ||
server: { | ||
protocol: 'http', | ||
host: 'localhost:3000', | ||
path: '/auth', | ||
state: true, | ||
}, | ||
facebook: { | ||
key: '353692268378789', | ||
secret: '30fa7be4ee732b4cc28c6d8acab54263', | ||
callback: `${GRANT_PATH}/handle_facebook_callback`, | ||
scope: [], | ||
}, | ||
google: { | ||
key: '822500959137-ktt8mfq95vlvq8ogcbu4gg3paear7174.apps.googleusercontent.com', | ||
secret: 'PBMWy2O-739OyqTItjXP3Dzq', | ||
callback: `${GRANT_PATH}/handle_google_callback`, | ||
scope: ['openid email'], | ||
}, | ||
}; |
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,45 @@ | ||
import * as request from 'request'; | ||
import { ServicesResolver } from '../types/types'; | ||
|
||
export class OAuthResolver { | ||
private servicesResolver: ServicesResolver = {}; | ||
|
||
setServicesResolver(resolver: ServicesResolver) { | ||
Object.assign(this.servicesResolver, resolver); | ||
} | ||
|
||
getServiceUrl(service: string): string { | ||
if (this.servicesResolver[service]) { | ||
return this.servicesResolver[service].url; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
async getUserFromServiceUserData(service: string, userData) { | ||
if (this.servicesResolver[service]) { | ||
return await this.servicesResolver[service].userResolver(userData); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
getUserDataFromService(accessToken: string, service: string) { | ||
return new Promise((resolve, reject) => { | ||
let userData = null; | ||
const requestUrl = this.getServiceUrl(service); | ||
if (!requestUrl) { | ||
reject('service is not defined'); | ||
} | ||
request(`${requestUrl}${accessToken}`, (error, response, body) => { | ||
if (!error && response.statusCode === 200) { | ||
userData = JSON.parse(body); | ||
resolve(userData); | ||
} | ||
else { | ||
reject(error); | ||
} | ||
}); | ||
}); | ||
}; | ||
} |
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,62 @@ | ||
import { OAuthResolver } from './oauth-resolver'; | ||
import AccountsServer from '@accounts/server'; | ||
import { getMongoClient } from '../db'; | ||
|
||
const oauthResolver: OAuthResolver = new OAuthResolver(); | ||
|
||
export const getOAuthResolver = () => { | ||
return oauthResolver; | ||
}; | ||
|
||
export const findUserByOAuthId = async (service: string, id) => { | ||
const mongoClient = await getMongoClient(); | ||
const dbCollection = await mongoClient.collection('users'); | ||
return await dbCollection.findOne({ [`profile.oauth.${service}`]: id }); | ||
}; | ||
|
||
export const addOAuthIdToUserProfile = async (user, service: string, serviceId) => { | ||
AccountsServer.setProfile(user.id, Object.assign({}, user.profile, | ||
{ | ||
oauth: { | ||
...user.profile.oauth, | ||
[service]: serviceId | ||
} | ||
})); | ||
}; | ||
|
||
export const initializeOAuthResolver = () => { | ||
oauthResolver.setServicesResolver({ | ||
google: { | ||
url: 'https://www.googleapis.com/plus/v1/people/me?access_token=', | ||
userResolver: async (userData) => { | ||
let user = await findUserByOAuthId('google', userData.id); | ||
if (user) { | ||
user.id = user._id; | ||
} | ||
else { | ||
user = await AccountsServer.findUserByEmail(userData.emails[0].value); | ||
} | ||
|
||
if (user) { | ||
await addOAuthIdToUserProfile(user, 'google', userData.id); | ||
} | ||
else { | ||
const id = await AccountsServer.createUser({ | ||
username: userData.emails[0].value, | ||
email: userData.emails[0].value, | ||
profile: { | ||
name: userData.name.givenName + ' ' + userData.name.familyName, | ||
avatar: userData.image.url, | ||
oauth: { | ||
google: userData.id, | ||
} | ||
} | ||
}); | ||
user = await AccountsServer.findUserById(id); | ||
} | ||
|
||
return user; | ||
}, | ||
}, | ||
}); | ||
}; |
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,4 +1,10 @@ | ||
declare module 'graphql' { | ||
export function subscribe(): any; | ||
|
||
export function execute(): any; | ||
} | ||
|
||
export interface ServicesResolver { | ||
[name: string]: { url: string, userResolver: (userData: any) => Promise<any> }; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move it inside the mock-server folder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
talked