Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Applied suggested changes from code review
  • Loading branch information
twsl committed Jul 22, 2019
commit 026b6b443766eb1763493ca0c27ceac519b4adea
2 changes: 1 addition & 1 deletion samples/Mvc.Client/Mvc.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="$(GoogleProviderVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Twitter" Version="$(TwitterProviderVersion)" />
</ItemGroup>
Expand Down
6 changes: 0 additions & 6 deletions samples/Mvc.Client/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ public void ConfigureServices(IServiceCollection services)
options.Scope.Add("user:email");
})

.AddGitLab(options =>
{
options.ClientId = "f4466c1688ecffbbcd9805cc0f0228c71da06d8d0554411259dfec3789711b59";
options.ClientSecret = "f540caad65deeac7dc43f9bc7343f834888d3ce8817a2ac211c2da0133d512ba";
})

.AddDropbox(options =>
{
options.ClientId = "jpk24g2uxfxe939";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
<Import Project="..\..\build\packages.props" />

<PropertyGroup>
<AspNetCoreVersion>2.2.0</AspNetCoreVersion>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<PropertyGroup>
<Description>ASP.NET Core security middleware enabling GitLab authentication.</Description>
<Authors>Kévin Chalet;Jerrie Pelser</Authors>
<PackageTags>aspnetcore;authentication;github;oauth;security</PackageTags>
<Authors>twsl</Authors>
<PackageTags>aspnetcore;authentication;gitlab;oauth;security</PackageTags>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
* for more information concerning the license and the contributors participating to this project.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth;

namespace AspNet.Security.OAuth.GitLab
{
/// <summary>
/// Default values for GitLab authentication
/// Default values used by the GitLab authentication middleware.
/// </summary>
public class GitLabAuthenticationDefaults
{
Expand Down
50 changes: 29 additions & 21 deletions src/AspNet.Security.OAuth.GitLab/GitLabAuthenticationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using AspNet.Security.OAuth.GitLab;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authentication;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -21,48 +18,59 @@ public static class GitLabAuthenticationExtensions
{
/// <summary>
/// Adds <see cref="GitLabAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Discord authentication capabilities.
/// <see cref="AuthenticationBuilder"/>, which enables GitLab authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static AuthenticationBuilder AddGitLab([NotNull] this AuthenticationBuilder builder)
=> builder.AddGitLab(GitLabAuthenticationDefaults.AuthenticationScheme, _ => { });

{
return builder.AddGitLab(GitLabAuthenticationDefaults.AuthenticationScheme, _ => { });
}

/// <summary>
/// Adds <see cref="GitLabAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Discord authentication capabilities.
/// <see cref="AuthenticationBuilder"/>, which enables GitLab authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static AuthenticationBuilder AddGitLab([NotNull] this AuthenticationBuilder builder, Action<GitLabAuthenticationOptions> configuration)
=> builder.AddGitLab(GitLabAuthenticationDefaults.AuthenticationScheme, configuration);

public static AuthenticationBuilder AddGitLab(
[NotNull] this AuthenticationBuilder builder,
[NotNull] Action<GitLabAuthenticationOptions> configuration)
{
return builder.AddGitLab(GitLabAuthenticationDefaults.AuthenticationScheme, configuration);
}

/// <summary>
/// Adds <see cref="GitLabAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Discord authentication capabilities.
/// <see cref="AuthenticationBuilder"/>, which enables GitLab authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="scheme">The authentication scheme associated with this instance.</param>
/// <param name="configuration">The delegate used to configure the Discord options.</param>
/// <param name="configuration">The delegate used to configure the GitLab options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddGitLab([NotNull] this AuthenticationBuilder builder, string scheme, Action<GitLabAuthenticationOptions> configuration)
=> builder.AddGitLab(scheme, GitLabAuthenticationDefaults.DisplayName, configuration);


public static AuthenticationBuilder AddGitLab(
[NotNull] this AuthenticationBuilder builder, [NotNull] string scheme,
[NotNull] Action<GitLabAuthenticationOptions> configuration)
{
return builder.AddGitLab(scheme, GitLabAuthenticationDefaults.DisplayName, configuration);
}

/// <summary>
/// Adds <see cref="GitLabAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Discord authentication capabilities.
/// <see cref="AuthenticationBuilder"/>, which enables GitLab authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="scheme">The authentication scheme associated with this instance.</param>
/// <param name="caption">The optional display name associated with this instance.</param>
/// <param name="configuration">The delegate used to configure the Discord options.</param>
/// <param name="configuration">The delegate used to configure the GitLab options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddGitLab([NotNull] this AuthenticationBuilder builder, string scheme, string caption, Action<GitLabAuthenticationOptions> configuration)
=> builder.AddOAuth<GitLabAuthenticationOptions, GitLabAuthenticationHandler>(scheme, caption, configuration);
public static AuthenticationBuilder AddGitLab(
[NotNull] this AuthenticationBuilder builder,
[NotNull] string scheme, [NotNull] string caption,
[NotNull] Action<GitLabAuthenticationOptions> configuration)
{
return builder.AddOAuth<GitLabAuthenticationOptions, GitLabAuthenticationHandler>(scheme, caption, configuration);
}
}
}
15 changes: 8 additions & 7 deletions src/AspNet.Security.OAuth.GitLab/GitLabAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
* for more information concerning the license and the contributors participating to this project.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
Expand All @@ -23,9 +20,14 @@ namespace AspNet.Security.OAuth.GitLab
{
public class GitLabAuthenticationHandler : OAuthHandler<GitLabAuthenticationOptions>
{
public GitLabAuthenticationHandler(IOptionsMonitor<GitLabAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
public GitLabAuthenticationHandler(
[NotNull] IOptionsMonitor<GitLabAuthenticationOptions> options,
[NotNull] ILoggerFactory logger,
[NotNull] UrlEncoder encoder,
[NotNull] ISystemClock clock)
: base(options, logger, encoder, clock)
{ }
{
}

protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIdentity identity, AuthenticationProperties properties, OAuthTokenResponse tokens)
{
Expand All @@ -37,7 +39,6 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIden
var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{

Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
"returned a {Status} response with the following payload: {Headers} {Body}.",
/* Status: */ response.StatusCode,
Expand Down
13 changes: 2 additions & 11 deletions src/AspNet.Security.OAuth.GitLab/GitLabAuthenticationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@
* for more information concerning the license and the contributors participating to this project.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Http;
using static AspNet.Security.OAuth.GitLab.GitLabAuthenticationConstants;

namespace AspNet.Security.OAuth.GitLab
Expand All @@ -23,10 +18,11 @@ public class GitLabAuthenticationOptions : OAuthOptions
/// </summary>
public GitLabAuthenticationOptions()
{
CallbackPath = new PathString(GitLabAuthenticationDefaults.CallbackPath);
CallbackPath = GitLabAuthenticationDefaults.CallbackPath;
AuthorizationEndpoint = GitLabAuthenticationDefaults.AuthorizationEndpoint;
TokenEndpoint = GitLabAuthenticationDefaults.TokenEndpoint;
UserInformationEndpoint = GitLabAuthenticationDefaults.UserInformationEndpoint;

Scope.Add("openid");
Scope.Add("profile");
Scope.Add("email");
Expand All @@ -39,10 +35,5 @@ public GitLabAuthenticationOptions()
ClaimActions.MapJsonKey(Claims.Avatar, "avatar_url");
ClaimActions.MapJsonKey(Claims.Url, "web_url");
}

/// <summary>
/// access_type. Set to 'offline' to request a refresh token.
/// </summary>
public string AccessType { get; set; }
}
}