An Oauth client handler, from abstract

vnext
Paul Schneider 6 years ago
parent 2c3d81b950
commit 3f153eb93a
18 changed files with 710 additions and 649 deletions

@ -2,7 +2,7 @@
namespace OAuth.AspNet.AuthServer
{
internal static class Constants
public static class Constants
{
public static class Parameters
{

@ -8,17 +8,14 @@ using System.Net;
using System.Text;
using GetUsernameAsyncFunc=System.Func<System.Collections.Generic.IDictionary<string,string>, System.Threading.Tasks.Task<string>>;
using System.IO;
using Microsoft.Extensions.WebEncoders;
using Newtonsoft.Json;
namespace cli.Authentication
namespace Yavsc.Authentication
{
public class OAuthenticator
{
UrlEncoder _urlEncoder;
public OAuthenticator(UrlEncoder urlEncoder)
public OAuthenticator()
{
_urlEncoder = urlEncoder;
}
string clientId;
@ -378,12 +375,12 @@ namespace cli.Authentication
/// </summary>
/// <param name="queryValues">The parameters to make the request with.</param>
/// <returns>The data provided in the response to the access token request.</returns>
protected async Task<IDictionary<string, string>> RequestAccessTokenAsync(IDictionary<string, string> queryValues)
public async Task<IDictionary<string, string>> RequestAccessTokenAsync(IDictionary<string, string> queryValues)
{
StringBuilder postData = new StringBuilder();
foreach (string key in queryValues.Keys)
{
postData.Append(_urlEncoder.UrlEncode($"{key}={queryValues[key]}&"));
postData.Append($"{key}="+Uri.EscapeDataString($"{queryValues[key]}")+"&");
}
var req = WebRequest.Create(accessTokenUrl);
(req as HttpWebRequest).Accept = "application/json";
@ -391,11 +388,8 @@ namespace cli.Authentication
var body = Encoding.UTF8.GetBytes(postData.ToString());
req.ContentLength = body.Length;
req.ContentType = "application/x-www-form-urlencoded";
using (var s = req.GetRequestStream())
{
s.Write(body, 0, body.Length);
s.Close();
}
var s = req.GetRequestStream();
s.Write(body, 0, body.Length);
var auth = await req.GetResponseAsync();
var repstream = auth.GetResponseStream();
@ -410,7 +404,7 @@ namespace cli.Authentication
if (data.ContainsKey("error"))
{
throw new AuthException("Error authenticating: " + data["error"]);
OnError("Error authenticating: " + data["error"]);
}
else if (data.ContainsKey("access_token"))
{
@ -418,8 +412,9 @@ namespace cli.Authentication
}
else
{
throw new AuthException("Expected access_token in access token response, but did not receive one.");
OnError("Expected access_token in access token response, but did not receive one.");
}
return data;
}
private IDictionary<string,string> FormDecode(string text)

@ -18,7 +18,7 @@ namespace Yavsc.Helpers
public static Stream GetStream(StreamReader reader)
{
var procStart = new ProcessStartInfo("/usr/bin/nodejs", "node_modules/ansi-to-html/bin/ansi-to-html");
var procStart = new ProcessStartInfo("node", "node_modules/ansi-to-html/bin/ansi-to-html");
procStart.UseShellExecute = false;
procStart.RedirectStandardInput = true;
procStart.RedirectStandardOutput = true;

@ -18,9 +18,10 @@ namespace Yavsc
{
private Client GetApplication(string clientId)
{
Client app=null;
using (var dbContext = new ApplicationDbContext()) {
app = dbContext.Applications.FirstOrDefault(x => x.Id == clientId);
Client app = null;
using (var dbContext = new ApplicationDbContext())
{
app = dbContext.Applications.FirstOrDefault(x => x.Id == clientId);
}
return app;
}
@ -54,9 +55,9 @@ namespace Yavsc
}
else
{
// if (client.Secret != Helper.GetHash(clientSecret))
// TODO store a hash in db, not the pass
if (client.Secret != clientSecret)
// if (client.Secret != Helper.GetHash(clientSecret))
// TODO store a hash in db, not the pass
if (client.Secret != clientSecret)
{
context.SetError("invalid_clientId", "Client secret is invalid.");
return Task.FromResult<object>(null);
@ -80,37 +81,35 @@ namespace Yavsc
else Startup.logger.LogWarning($"ValidateClientAuthentication: neither Basic nor Form credential were found");
return Task.FromResult(0);
}
UserManager<ApplicationUser> _usermanager;
private async Task<Task> GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
logger.LogWarning($"GrantResourceOwnerCredentials task ... {context.UserName}");
ApplicationUser user = null;
using (var usermanager = context.HttpContext.ApplicationServices.GetRequiredService<UserManager<ApplicationUser>>())
{
user = await usermanager.FindByNameAsync(context.UserName);
if (await usermanager.CheckPasswordAsync(user,context.Password))
{
var claims = new List<Claim>(
context.Scope.Select(x => new Claim("urn:oauth:scope", x))
);
claims.Add(new Claim(ClaimTypes.NameIdentifier,user.Id));
claims.Add(new Claim(ClaimTypes.Email,user.Email));
claims.AddRange((await usermanager.GetRolesAsync(user)).Select(
r => new Claim(ClaimTypes.Role,r)
) );
ClaimsPrincipal principal = new ClaimsPrincipal(
new ClaimsIdentity(
new GenericIdentity(context.UserName, OAuthDefaults.AuthenticationType),
claims)
);
// TODO set a NameIdentifier, roles and scopes claims
context.HttpContext.User = principal;
user = await _usermanager.FindByNameAsync(context.UserName);
if (await _usermanager.CheckPasswordAsync(user, context.Password))
{
context.Validated(principal);
}
}
var claims = new List<Claim>(
context.Scope.Select(x => new Claim("urn:oauth:scope", x))
);
claims.Add(new Claim(ClaimTypes.NameIdentifier, user.Id));
claims.Add(new Claim(ClaimTypes.Email, user.Email));
claims.AddRange((await _usermanager.GetRolesAsync(user)).Select(
r => new Claim(ClaimTypes.Role, r)
));
ClaimsPrincipal principal = new ClaimsPrincipal(
new ClaimsIdentity(
new GenericIdentity(context.UserName, OAuthDefaults.AuthenticationType),
claims)
);
// TODO set a NameIdentifier, roles and scopes claims
context.HttpContext.User = principal;
context.Validated(principal);
}
return Task.FromResult(0);
}

@ -27,6 +27,7 @@ namespace Yavsc
using System.Net;
using Formatters;
using Google.Apis.Util.Store;
using Microsoft.AspNet.Identity;
using Microsoft.Extensions.Localization;
using Models;
using PayPal.Manager;
@ -263,9 +264,10 @@ namespace Yavsc
IOptions<PayPalSettings> payPalSettings,
IOptions<GoogleAuthSettings> googleSettings,
IStringLocalizer<Yavsc.Resources.YavscLocalisation> localizer,
UserManager<ApplicationUser> usermanager,
ILoggerFactory loggerFactory)
{
_usermanager = usermanager;
GoogleSettings = googleSettings.Value;
ResourcesHelpers.GlobalLocalizer = localizer;
SiteSetup = siteSettings.Value;

@ -31,10 +31,6 @@ gulp.task("clean:css", function(cb) {
gulp.task("clean", ["clean:js", "clean:css"]);
gulp.task('watch', shell.task(['ASPNET_ENV=Development dnx-watch web --configuration=Debug']));
gulp.task('watchlua', shell.task(['ASPNET_ENV=lua dnx-watch luatest --configuration=Debug']));
gulp.task('watchpre', shell.task(['ASPNET_ENV=yavscpre dnx-watch web --configuration=Debug']));
gulp.task("min:css", function() {
gulp.src([paths.css, "!" + paths.minCss, '!site.css'])
.pipe(cssmin())
@ -56,4 +52,4 @@ gulp.task('run', shell.task(['ASPNET_ENV=Development dnx web --configuration=Deb
gulp.task('buildrelease', shell.task(['dnu build --configuration=Release']));
gulp.task("default", ["watch"]);
gulp.task("default", ["min"]);

1004
Yavsc/package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,26 +1,26 @@
{
"name": "yavsc",
"version": "1.0.1",
"description": "Yet Another Very Small Company",
"repository": {
"type": "Git",
"url": "https://github.com/pazof/yavsc"
},
"license": "GPL-3.0",
"devDependencies": {
"grunt": "^1.0.2",
"gulp": "^3.9.1",
"gulp-clean": "^0.4.0",
"gulp-concat": "2.6.1",
"gulp-cssmin": "^0.2.0",
"gulp-dnx-tasks": "^1.0.0-beta7",
"gulp-shell": "^0.6.5",
"gulp-uglify": "^3.0.0",
"rimraf": "^2.6.2"
},
"dependencies": {
"ansi-to-html": "^0.6.4",
"braintree-web": "^3.31.0",
"paypal-permissions-sdk": "^1.0.10"
}
"name": "yavsc",
"version": "1.0.1",
"description": "Yet Another Very Small Company",
"repository": {
"type": "Git",
"url": "https://github.com/pazof/yavsc"
},
"license": "GPL-3.0",
"devDependencies": {
"grunt": "^1.0.2",
"gulp": "^3.9.1",
"gulp-clean": "^0.4.0",
"gulp-concat": "2.6.1",
"gulp-cssmin": "^0.2.0",
"gulp-dnx-tasks": "^1.0.0-beta7",
"gulp-shell": "^0.6.5",
"gulp-uglify": "^3.0.0",
"rimraf": "^2.6.2"
},
"dependencies": {
"ansi-to-html": "^0.6.4",
"braintree-web": "^3.31.0",
"paypal-permissions-sdk": "^1.0.10"
}
}

@ -29,6 +29,7 @@
"Microsoft.Extensions.Configuration.Abstractions": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
"Microsoft.Extensions.CommandLineUtils": "1.1.1",
"Microsoft.Extensions.DependencyInjection": "1.0.0-rc1-final",
"Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-rc1-final",
"Microsoft.Extensions.Globalization.CultureInfoCache": "1.0.0-rc1-final",

@ -1,32 +0,0 @@
using System;
using System.Runtime.Serialization;
namespace cli.Authentication
{
[Serializable]
internal class AuthException : Exception
{
private object p;
public AuthException()
{
}
public AuthException(object p)
{
this.p = p;
}
public AuthException(string message) : base(message)
{
}
public AuthException(string message, Exception innerException) : base(message, innerException)
{
}
protected AuthException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}

@ -61,6 +61,7 @@ namespace cli
services.AddTransient(typeof(RazorEngineHost), typeof(YaRazorEngineHost));
services.AddEntityFramework().AddNpgsql().AddDbContext<ApplicationDbContext>();
services.AddTransient((s) => new RazorTemplateEngine(s.GetService<RazorEngineHost>()));
// services.AddTransient<,>()
services.AddLogging();
services.AddTransient<EMailer>();
services.AddLocalization(options =>

@ -6,16 +6,24 @@ all: test
project.lock.json: project.json
dnu restore
$(BINTARGET): project.lock.json
dnu build --configuration $(CONFIGURATION)
../Yavsc/bin/$(CONFIGURATION)/dnx451/Yavsc.dll:
make -C ../Yavsc
../Yavsc.Abstract/bin/$(CONFIGURATION)/dnx451/Yavsc.Abstract.dll:
make -C ../Yavsc.Abstract
../Yavsc.Server/bin/$(CONFIGURATION)/dnx451/Yavsc.Server.dll:
make -C ../Yavsc.Server
$(BINTARGET): project.lock.json ../Yavsc/bin/$(CONFIGURATION)/dnx451/Yavsc.dll ../Yavsc.Abstract/bin/$(CONFIGURATION)/dnx451/Yavsc.Abstract.dll ../Yavsc.Server/bin/$(CONFIGURATION)/dnx451/Yavsc.Server.dll
dnu build --configuration $(CONFIGURATION)
breaking:
dnx test -class test.YavscWorkInProgress.GitClone
dnx test -trait noregres=no
test: $(BINTARGET)
ASPNET_ENV=Development dnx test -maxthreads 1
ASPNET_ENV=Development dnx test -maxthreads 1 -trait noregres=yes
.PHONY: test

@ -7,7 +7,28 @@
"HomeViewName": "Home",
"FavIcon": "/favicon.ico",
"Icon": "/images/yavsc.png",
"GitRepository": "testingrepo"
"GitRepository": "testingrepo",
"Owner": {
"Name": "Paul",
"EMail": "paul@pschneider.fr",
"PostalAddress": {
"Street1": "2 Blv A. Briand",
"Street2": "Apt 284 - Bat V",
"PostalCode": "92150",
"City": "Suresnes",
"State": "France",
"Province": null
}
},
"Admin": {
"Name": "Paul",
"EMail": "contact@pschneider.fr"
}
},
"Smtp": {
"Host": "localhost",
"Port": 25,
"EnableSSL": false
},
"Logging": {
"IncludeScopes": true,

@ -0,0 +1,20 @@
0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Programs\\nodejs\\node.exe',
1 verbose cli 'C:\\Programs\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'install' ]
2 info using npm@1.4.9
3 info using node@v8.11.3
4 error install Couldn't read dependencies
5 error package.json ENOENT: no such file or directory, open 'C:\Users\paul\Documents\GitHub\yavsc\test\package.json'
5 error package.json This is most likely not a problem with npm itself.
5 error package.json npm can't find a package.json file in your current directory.
6 error System Windows_NT 6.1.7601
7 error command "C:\\Programs\\nodejs\\node.exe" "C:\\Programs\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install"
8 error cwd C:\Users\paul\Documents\GitHub\yavsc\test
9 error node -v v8.11.3
10 error npm -v 1.4.9
11 error path C:\Users\paul\Documents\GitHub\yavsc\test\package.json
12 error syscall open
13 error code ENOPACKAGEJSON
14 error errno -4058
15 verbose exit [ -4058, true ]

@ -7,6 +7,8 @@ using Yavsc.Helpers;
namespace test
{
[Trait("noregres", "yes")]
public class YavscDnxUnitTests
{
[Fact]
@ -43,7 +45,7 @@ namespace test
public static Stream GetStream(StreamReader reader)
{
var procStart = new ProcessStartInfo("/usr/bin/nodejs", "node_modules/ansi-to-html/bin/ansi-to-html");
var procStart = new ProcessStartInfo("node", "node_modules/ansi-to-html/bin/ansi-to-html");
procStart.UseShellExecute = false;
procStart.RedirectStandardInput = true;
procStart.RedirectStandardOutput = true;

@ -20,6 +20,8 @@ using Yavsc.Helpers;
using Microsoft.Data.Entity;
using Xunit.Abstractions;
using System.IO;
using Yavsc.Lib;
using System.Linq;
namespace test
{
@ -35,33 +37,30 @@ namespace test
}
[Collection("Yavsc mandatory success story")]
public class YavscMandatory: BaseTestContext
[Trait("noregres", "yes")]
public class YavscMandatory: BaseTestContext, IClassFixture<ServerSideFixture>
{
private readonly ITestOutputHelper output;
public YavscMandatory(ITestOutputHelper output)
private readonly ITestOutputHelper _output;
ServerSideFixture _serverFixture;
public YavscMandatory(ITestOutputHelper output, ServerSideFixture serverFixture)
{
this.output = output;
this._output = output;
this._serverFixture = serverFixture;
}
[Fact]
void TestStartNodeJsansitohtml()
public void GitClone()
{
var procStart = new ProcessStartInfo("env", "/usr/bin/nodejs node_modules/ansi-to-html/bin/ansi-to-html");
procStart.UseShellExecute = false;
procStart.RedirectStandardInput = true;
procStart.RedirectStandardOutput = true;
procStart.RedirectStandardError = true;
var proc = Process.Start(procStart);
proc.StandardInput.WriteLine("\x001b[30mblack\x1b[37mwhite");
proc.StandardInput.Close();
while (!proc.StandardOutput.EndOfStream)
{
output.WriteLine(proc.StandardOutput.ReadLine());
}
var dbc = _serverFixture._app.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
var firstProject = dbc.Projects.Include(p=>p.Repository).FirstOrDefault();
Assert.NotNull (firstProject);
var clone = new GitClone(_serverFixture._siteSetup.GitRepository);
clone.Launch(firstProject);
}
// actually uses node's ansi-to-html
[Fact]
void AnsiToHtml()
{
@ -74,7 +73,7 @@ namespace test
using (var reader = new StreamReader(encoded))
{
var txt = reader.ReadToEnd();
output.WriteLine(txt);
_output.WriteLine(txt);
}
}
@ -109,8 +108,8 @@ namespace test
public void AConfigurationRoot()
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile(Path.Combine(testprojectAssetPath, "appsettings.json"), false);
builder.AddJsonFile(Path.Combine(testprojectAssetPath, "appsettings.Development.json"), true);
builder.AddJsonFile( "appsettings.json", false);
builder.AddJsonFile( "appsettings.Development.json", true);
configurationRoot = builder.Build();
}

@ -3,7 +3,10 @@
// paul 21/06/2018 10:11 20182018 6 21
// */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Builder.Internal;
using Microsoft.Data.Entity;
@ -14,15 +17,19 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.OptionsModel;
using Microsoft.Extensions.PlatformAbstractions;
using Newtonsoft.Json;
using Xunit;
using Xunit.Abstractions;
using Yavsc;
using Yavsc.Authentication;
using Yavsc.Lib;
using Yavsc.Models;
using static OAuth.AspNet.AuthServer.Constants;
namespace test
{
[Collection("Yavsc Work In Progress")]
[Trait("noregres", "no")]
public class YavscWorkInProgress : BaseTestContext, IClassFixture<ServerSideFixture>
{
@ -34,17 +41,59 @@ namespace test
_serverFixture = serverFixture;
}
[Fact]
public void GitClone()
{
var dbc = _serverFixture._app.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
var firstProject = dbc.Projects.Include(p=>p.Repository).FirstOrDefault();
Assert.NotNull (firstProject);
[Theory]
[InlineData("d9be5e97-c19d-42e4-b444-0e65863b19e1","blouh","profile",
"http://localhost:5000/authorize",
"http://localhost:5000/oauth/success",
"http://localhost:5000/token",
"joe",
"badpass"
)]
public async Task TestUserMayLogin
(
string clientId,
string clientSecret,
string scope,
string authorizeUrl,
string redirectUrl,
string accessTokenUrl,
string login,
string pass
)
{
try {
var r = new Uri(redirectUrl);
var oauthor =new OAuthenticator( clientId, clientSecret, scope,
new Uri( authorizeUrl) , new Uri(redirectUrl) , new Uri(accessTokenUrl));
var query = new Dictionary<string,string>();
query[Parameters.Username]=login;
query[Parameters.Password]=pass;
query[Parameters.ClientId]=clientId;
query[Parameters.ClientSecret]=clientSecret;
query[Parameters.Scope]=scope;
query[Parameters.GrantType]=GrantTypes.Password;
var result = await oauthor.RequestAccessTokenAsync(query);
Console.WriteLine(">> Got an output");
Console.WriteLine(Parameters.AccessToken+": "+result[Parameters.AccessToken]);
Console.WriteLine(Parameters.TokenType+": "+result[Parameters.TokenType]);
Console.WriteLine(Parameters.ExpiresIn+": "+result[Parameters.ExpiresIn]);
Console.WriteLine(Parameters.RefreshToken+": "+result[Parameters.RefreshToken]);
var clone = new GitClone(_serverFixture._siteSetup.GitRepository);
clone.Launch(firstProject);
}
catch (Exception ex)
{
var webex = ex as WebException;
if (webex!=null && webex.Status == (WebExceptionStatus)400)
{
if (login == "joe") {
Console.WriteLine("Bad pass joe!");
return;
}
}
throw;
}
}
}
}

@ -8,4 +8,4 @@ DESTDIR=$(SOLUTIONDIR)/build
MAKE=make
NUGETSOURCE=$(HOME)/Nupkgs
VERSION=1.0.5-rc$(rc_num)
CONFIGURATION=Release
CONFIGURATION=Debug

Loading…