-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
02c3681
commit 30f78ef
Showing
8 changed files
with
191 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Router } from 'express'; | ||
import { AtlassianStrategyType } from '../../middlewares/passport/passport-strategy-type'; | ||
import { login, logout } from '../../controllers/auth-controller'; | ||
import passport from '../../strategies/passport.strategy'; | ||
|
||
const router = Router(); | ||
|
||
router.get('/',passport.authenticate('atlassian')); | ||
|
||
router.get('/callback', AtlassianStrategyType, login); | ||
|
||
router.get('/logout', logout); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './auth/stack-exchange-auth.routes'; | ||
export * from './auth/github-auth.routes'; | ||
export * from './auth/bitbucket.auth.routes'; | ||
export * from './stack-exchange/search.routes'; | ||
export * from './github/search.routes'; | ||
export * from './bitbucket/search.routes'; |
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,148 @@ | ||
import OAuth2Strategy from 'passport-oauth2'; | ||
|
||
const defaultOptions = { | ||
authorizationURL: 'https://auth.atlassian.com/authorize', | ||
tokenURL: 'https://auth.atlassian.com/oauth/token', | ||
profileURL: 'https://api.atlassian.com/me', | ||
accessibleResourcesURL: 'https://api.atlassian.com/oauth/token/accessible-resources', | ||
}; | ||
|
||
const defaultScope = ['read:me']; | ||
|
||
class Strategy extends OAuth2Strategy { | ||
/** | ||
* `Strategy` constructor. | ||
* | ||
* Atlassian strategy authenticates requests using the OAuth 2.0 (3LO) protocol. | ||
* | ||
* Applications must supply a `verify` callback which accepts an `accessToken`, | ||
* `refreshToken` and service-specific `profile`, and then calls the `cb` | ||
* callback supplying a `user`, which should be set to `false` if thed | ||
* credentials are not valid. If an exception occured, `err` should be set. | ||
* | ||
* Options: | ||
* - `clientID` your Atlassian application's App ID | ||
* - `clientSecret` your Atlassian application's App Secret | ||
* - `callbackURL` URL to which Atlassian will redirect the user after granting authorization | ||
* - `scope` API scopes that enable access to certain resources | ||
* | ||
* Examples: | ||
* | ||
* passport.use(new AtlassianStrategy({ | ||
* clientID: '123-456-789', | ||
* clientSecret: 'secret', | ||
* callbackURL: 'https://www.example.net/auth/atlassian/callback', | ||
* scope: 'offline_access read:jira-user' | ||
* }, function(accessToken, refreshToken, profile, cb) { | ||
* User.findOrCreate(..., function (err, user) { | ||
* cb(err, user); | ||
* }); | ||
* })); | ||
* | ||
* @constructor | ||
* @param {object} options | ||
* @param {function} verify | ||
* @access public | ||
*/ | ||
private options; | ||
public name='atlassian'; | ||
private _skipUserProfile:any; | ||
private _scope:any; | ||
private _scopeSeparator:any; | ||
|
||
|
||
constructor(options:any = {}, verify:any) { | ||
if (!options?.scope) { | ||
throw new TypeError('AtlassianOAuth2 requires a scope option'); | ||
} | ||
|
||
options = { | ||
...defaultOptions, | ||
...options, | ||
}; | ||
|
||
|
||
|
||
super(options, verify); | ||
this.options = options; | ||
this.name = 'atlassian'; | ||
this._oauth2.useAuthorizationHeaderforGET(true); | ||
this._setupDefaultScope(); | ||
} | ||
|
||
/** | ||
* Return extra Atlassian OAuth2 specific parameters to be included in the authorization | ||
* request. | ||
* | ||
* @return {object} | ||
* @access protected | ||
*/ | ||
authorizationParams() { | ||
return { | ||
audience: 'api.atlassian.com', | ||
prompt: 'consent', | ||
}; | ||
} | ||
|
||
/** | ||
* Return normalized user profile | ||
* | ||
* @param {string} accessToken | ||
* @param {function} done | ||
* @access protected | ||
*/ | ||
userProfile(accessToken:any, done:any) { | ||
this._oauth2.get(this.options.profileURL, accessToken, (err:any, body:any) => { | ||
if (err) return done(err); | ||
|
||
let profile:any; | ||
|
||
try { | ||
const json = JSON.parse(body); | ||
profile = this._convertProfileFields(json); | ||
profile.provider = 'atlassian'; | ||
profile._raw = body; | ||
profile._json = json; | ||
} catch (e) { | ||
return done(new Error('Failed to parse user profile')); | ||
} | ||
|
||
// Retreive list of resources user has access to | ||
this._oauth2.get(this.options.accessibleResourcesURL, accessToken, (errAr:any, bodyAr:any) => { | ||
if (errAr) return done(errAr); | ||
|
||
try { | ||
profile.accessibleResources = JSON.parse(bodyAr); | ||
done(null, profile); | ||
} catch (e) { | ||
return done(new Error('Failed to parse accessible resources')); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
_setupDefaultScope() { | ||
if (this._skipUserProfile) { | ||
return; | ||
} | ||
|
||
let scope = this._scope || []; | ||
|
||
if (!Array.isArray(scope)) { | ||
scope = scope.split(this._scopeSeparator); | ||
} | ||
|
||
this._scope = Array.from(new Set([...scope, ...defaultScope])); | ||
} | ||
|
||
_convertProfileFields(json:any) { | ||
return { | ||
id: json.account_id, | ||
displayName: json.name, | ||
emails: [{ value: json.email, verified: json.email_verified }], | ||
photos: [{ value: json.picture }], | ||
}; | ||
} | ||
} | ||
|
||
export default Strategy; |
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,2 +1,3 @@ | ||
declare module 'passport'; | ||
declare module 'passport-oauth' | ||
declare module 'passport-oauth'; | ||
// declare module 'passport-atlassian-oauth2'; |