Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ app.get('/auth/gitlab',
}));
```

##### How can I fetch the groups of the logged-in user?

If the OAuth2-scope is set to `read_user api`, the user's groups are also
requested and included in the `groups` property of the returned object.
Groups are saved as an array of the group names represented as strings.

More information can be found in the [official GitLab documentation](https://docs.gitlab.com/ce/integration/oauth_provider.html#authorized-applications).

## Contributing
Expand Down
24 changes: 23 additions & 1 deletion lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function Strategy(options, verify) {
OAuth2Strategy.call(this, options, verify);
this.name = 'gitlab';
this._profileURL = options.profileURL || url.resolve(this._baseURL, 'api/v4/user');
this._groupsURL = options.groupsURL || url.resolve(this._baseURL, 'api/v4/groups?min_access_level=10');
this._oauth2.useAuthorizationHeaderforGET(true);
}

Expand All @@ -65,6 +66,7 @@ util.inherits(Strategy, OAuth2Strategy);
* - `username` the user's GitLab username
* - `displayName` the user's full name
* - `emails` the proxied or contact email address granted by the user
* - `groups` the user's groups
* - `avatarUrl` the URL of the GitLab profile picture
* - `profileUrl` the URL to the GitLab profile of the user
*
Expand Down Expand Up @@ -105,7 +107,27 @@ Strategy.prototype.userProfile = function(accessToken, done) {
profile._raw = body;
profile._json = json;

done(null, profile);
if (self._scope.includes('api')) {
self._oauth2.get(self._groupsURL, accessToken, function(err, body) {
let jsonGroups;

if (err) {
return done(new InternalOAuthError('Failed to fetch groups of user', err));
}

try {
jsonGroups = JSON.parse(body);
} catch (ex) {
return done(new Error('Failed to parse groups of user'));
}

profile.groups = jsonGroups.map((g) => g.name);

done(null, profile);
});
} else {
done(null, profile);
}
});
};

Expand Down
Loading