Last active
May 28, 2018 07:36
-
-
Save emyann/73efccd03eb1848010ebafb9b12fc76d to your computer and use it in GitHub Desktop.
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
using Microsoft.AspNet.Identity.EntityFramework; | |
using Microsoft.Owin.Security; | |
using Microsoft.Owin.Security.OAuth; | |
using Pollen.Radar.Model; | |
using Pollen.Radar.Service; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Security.Claims; | |
using System.Threading.Tasks; | |
using System.Web; | |
namespace Pollen.Radar.API | |
{ | |
public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider | |
{ | |
protected AuthenticationService AccountService { get; set; } | |
public SimpleAuthorizationServerProvider() | |
{ | |
this.AccountService = new AuthenticationService(); | |
} | |
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) | |
{ | |
string clientId = string.Empty; | |
string clientSecret = string.Empty; | |
Client client = null; | |
if (!context.TryGetBasicCredentials(out clientId, out clientSecret)) | |
{ | |
context.TryGetFormCredentials(out clientId, out clientSecret); | |
} | |
if (context.ClientId == null) | |
{ | |
//Remove the comments from the below line context.SetError, and invalidate context | |
//if you want to force sending clientId/secrects once obtain access tokens. | |
//context.Validated(); | |
context.SetError("invalid_clientId", "ClientId should be sent."); | |
return Task.FromResult<object>(null); | |
} | |
client = this.AccountService.FindClient(context.ClientId); | |
if (client == null) | |
{ | |
context.SetError("invalid_clientId", string.Format("Client '{0}' is not registered in the system.", context.ClientId)); | |
return Task.FromResult<object>(null); | |
} | |
if (client.ApplicationType == ApplicationType.NativeConfidential) | |
{ | |
if (string.IsNullOrWhiteSpace(clientSecret)) | |
{ | |
context.SetError("invalid_clientId", "Client secret should be sent."); | |
return Task.FromResult<object>(null); | |
} | |
else | |
{ | |
if (client.Secret != clientSecret.GetHash()) | |
{ | |
context.SetError("invalid_clientId", "Client secret is invalid."); | |
return Task.FromResult<object>(null); | |
} | |
} | |
} | |
if (!client.Active) | |
{ | |
context.SetError("invalid_clientId", "Client is inactive."); | |
return Task.FromResult<object>(null); | |
} | |
context.OwinContext.Set<string>("as:clientAllowedOrigin", client.AllowedOrigin); | |
context.OwinContext.Set<string>("as:clientRefreshTokenLifeTime", client.RefreshTokenLifeTime.ToString()); | |
context.Validated(); | |
return Task.FromResult<object>(null); | |
} | |
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) | |
{ | |
var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin"); | |
if (allowedOrigin == null) allowedOrigin = "*"; | |
if (context.OwinContext.Response.Headers.ContainsKey("Access-Control-Allow-Origin")) | |
{ | |
context.OwinContext.Response.Headers.SetValues("Access-Control-Allow-Origin", new[] { allowedOrigin }); | |
} | |
else | |
{ | |
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin }); | |
} | |
var username = context.UserName; | |
if (context.UserName.Contains("@")) | |
{ | |
var userForEmail = await this.AccountService.FindByEmailAsync(context.UserName); | |
if (userForEmail != null) | |
{ | |
username = userForEmail.UserName; | |
} | |
} | |
IdentityUser user = await this.AccountService.FindUser(username, context.Password); | |
if (user == null) | |
{ | |
context.SetError("invalid_grant", "The user name or password is incorrect."); | |
return; | |
} | |
var identity = new ClaimsIdentity(context.Options.AuthenticationType); | |
identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName)); | |
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id)); | |
identity.AddClaim(new Claim("sub", context.UserName)); | |
identity.AddClaim(new Claim("role", "user")); | |
var props = new AuthenticationProperties(new Dictionary<string, string> | |
{ | |
{ | |
"as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId | |
}, | |
{ | |
"userName", context.UserName | |
}, | |
{ | |
"email", user.Email | |
} | |
}); | |
var ticket = new AuthenticationTicket(identity, props); | |
context.Validated(ticket); | |
} | |
public override Task TokenEndpoint(OAuthTokenEndpointContext context) | |
{ | |
foreach (KeyValuePair<string, string> property in context.Properties.Dictionary) | |
{ | |
context.AdditionalResponseParameters.Add(property.Key, property.Value); | |
} | |
return Task.FromResult<object>(null); | |
} | |
public override Task GrantRefreshToken(OAuthGrantRefreshTokenContext context) | |
{ | |
var originalClient = context.Ticket.Properties.Dictionary["as:client_id"]; | |
var currentClient = context.ClientId; | |
if (originalClient != currentClient) | |
{ | |
context.SetError("invalid_clientId", "Refresh token is issued to a different clientId."); | |
return Task.FromResult<object>(null); | |
} | |
// Change auth ticket for refresh token requests | |
var newIdentity = new ClaimsIdentity(context.Ticket.Identity); | |
newIdentity.AddClaim(new Claim("newClaim", "newValue")); | |
var newTicket = new AuthenticationTicket(newIdentity, context.Ticket.Properties); | |
context.Validated(newTicket); | |
return Task.FromResult<object>(null); | |
} | |
public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context) | |
{ | |
if (context.ClientId == "WebApp") | |
{ | |
context.Validated(); | |
} | |
return Task.FromResult<object>(null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you clarify with a comment what line 163 does? Is this dummy code and should I be adding something else here? Or is "newClaim" really a thing?