Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

14 Commits

Repository files navigation

NebulaAuthServer

Lightweight authorization framework

Project structure

Cyaim.Authentication:authorization common library.
NebulaAuthServer.AccessManagement:All standard,authorization model it's here.
NebulaAuthServer:Authorization center.

Quick Start

1,Install Nuget package

Project Nuget address:
https://www.nuget.org/packages/Cyaim.Authentication/

Nuget run command.

Install-PackageCyaim.Authentication-Version1.0.0

2,Add auth middleware & configure auth service

In "Startup.cs" -> "ConfigureServices" Method,the method last add code.

services.ConfigureAuth(x =>{x.SourceLocation=ParameterLocation.Header;x.PreAccessEndPointKey="Sys";});

In "Configure" Method add middleware.

AuthMiddleware must add above UseEndpoints.

app.UseAuth();

3,Go to controller

In need of authorization Controller or Action mark:

[AuthEndPoint()]

In not authorization Action mark:

[AuthEndPoint(allowGuest:true)]

4,Use Attribute Complate,Run you project~

Persistence and cache

1,You need a class verify authorization

Here operation PostgreSQL and redis, You can replace it with something you like.

usingSystem;usingSystem.IO;usingSystem.Linq;usingSystem.Threading.Tasks;usingNewtonsoft.Json;usingstaticCyaim.Authentication.Infrastructure.Helpers.URLStructHelper;publicclassAuth{staticAuth(){_npgsqlDapperHelper=NpgsqlDapperHelper.Helper("Your databse connection string");_redisHelper=newRedisHelper("Your redis connection string","Auth",0);}privatereadonlystaticNpgsqlDapperHelper_npgsqlDapperHelper;privatereadonlystaticRedisHelper_redisHelper;conststringROUTE="/api/v1/{controller}/{action}";publicstaticasyncTask<AuthEndPointAttribute[]>GetAuthEndPointByUser(stringauthKey,HttpContexthttpContext,AuthOptionsauthOptions){stringpersonId=GetUserIdByRedis(authKey);if(personId==null){// to do request is not loginpersonId="sys_guest";}URLStructurlStruct=GetUrlStruct(ROUTE,httpContext.Request.Path);// Adopt request URL search watching endpointvarwatchep=authOptions.WatchAuthEndPoint.FirstOrDefault(x =>x.Routes!=null&&x.ControllerName?.ToLower()==urlStruct.Controller.ToLower()+"controller"&&x.Routes.Any(r =>r.Template?.ToLower()==urlStruct.Action?.ToLower()));if(watchep==null){//This means that the request is not listening//goto GoNonAccess;Console.WriteLine($@"Endpoint -> {httpContext.Request.Path} not databse watching range.");gotoNonAccessWatch;}//----------Begin database query user authorization code----------if(user permission ==false){gotoGoNonAccess;}//----------End your code----------returnnewAuthEndPointAttribute[1]{watchep};//Non AccessGoNonAccess:returnnewAuthEndPointAttribute[0];//Non watching rangeNonAccessWatch:returnnull;}/// <summary>/// From redis get token/// </summary>/// <param name="authKey"></param>publicstaticstringGetUserIdByRedis(stringauthKey){varpersonId=_redisHelper.StringGet(authKey);returnpersonId;}}

2,Add Auth Handler

In "Startup.cs" -> "ConfigureServices" Method,the method last replace code.

services.ConfigureAuth(x =>{x.SourceLocation=ParameterLocation.Header;x.ExtractDatabaseAuthEndPoints=newAuthOptions.ExtractAuthEndPointsHandler(Auth.GetAuthEndPointByUser);x.PreAccessEndPointKey="Sys";});

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages