Git Product home page Git Product logo

identity-as-a-service's Introduction

identity-as-a-service workshop

This workshop follows examples from https://identityserver4.readthedocs.io/en/latest/index.html It's a continuation of https://github.com/karpikpl/oidc-spa-client

Build sample Identity As A Service project

dotnet new -i IdentityServer4.Templates

mkdir idaas && cd idaas
mkdir src && cd src

dotnet new is4empty -n IdentityServer
cd ..

dotnet new sln -n idaas
dotnet sln add .\src\IdentityServer\IdentityServer.csproj

Protect the API

In config.cs add an API resource:

public static IEnumerable<ApiResource> GetApis()
{
    return new ApiResource[] 
    {
        new ApiResource("api", "Acme Fireworks Co. payroll")
    };
}

add a client:

return new Client[]
{
    new Client
    {
        ClientId = "client",
        // no interactive user, use the clientid/secret for authentication
        AllowedGrantTypes = GrantTypes.ClientCredentials,
        // secret for authentication
        ClientSecrets =
        {
            new Secret("secret".Sha256())
        },
        // scopes that client has access to
        AllowedScopes = { "api" }
    }
};

Change Authority in the API project

options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.Audience = "api";

Get the token

Get the token

curl -v -X POST http://localhost:5000/connect/token -d "client_id=client&client_secret=secret&grant_type=client_credentials&scope=api" | json_pp

Call the API

curl https://localhost:6001/api/secure -v -k --header "Authorization: Bearer xxx" | json_pp

where xxx is the access_token

Inspect the token

http://jwt.io

Add UI to IdentityServer

cd src/IdentityServer
dotnet new is4ui

In startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    // uncomment, if you want to add an MVC-based UI
    services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1);

and

// uncomment if you want to support static files
app.UseStaticFiles();

app.UseIdentityServer();

// uncomment, if you want to add an MVC-based UI
app.UseMvcWithDefaultRoute();

Setup CORS

In startup.cs - public void ConfigureServices(IServiceCollection services)

var cors = new DefaultCorsPolicyService(new LoggerFactory().CreateLogger<DefaultCorsPolicyService>())
{
    AllowAll = true
};
services.AddSingleton<ICorsPolicyService>(cors);

Register UI Client

Add identity resources in config.cs:

return new IdentityResource[]
{
    new IdentityResources.OpenId(),
    new IdentityResources.Profile(),
    new IdentityResources.Email()
};

Add SPA client:

new Client
  {
      ClientId = "spa",
      ClientName = "Single Page Javascript App",
      AllowedGrantTypes = GrantTypes.Code,
      // Specifies whether this client can request refresh tokens
      AllowOfflineAccess = true,
      RequireClientSecret = false,

      // where to redirect to after login
      RedirectUris = { "http://localhost:8080/callback.html" },

      // where to redirect to after logout
      PostLogoutRedirectUris = { "http://localhost:8080/logout.html" },

      AllowedScopes = new List<string>
      {
          IdentityServerConstants.StandardScopes.OpenId,
          IdentityServerConstants.StandardScopes.Profile,
          IdentityServerConstants.StandardScopes.Email,
          "api"
      }

Add Users

In startup.cs:

var builder = services.AddIdentityServer()
    .AddInMemoryIdentityResources(Config.GetIdentityResources())
    .AddInMemoryApiResources(Config.GetApis())
    .AddInMemoryClients(Config.GetClients())
    .AddTestUsers(Config.GetTestUsers());

In config.cs:

internal static List<TestUser> GetTestUsers()
{
    return new List<TestUser>
    {
        new TestUser { SubjectId = "1", Username = "alice", Password = "alice",
            Claims =
            {
                new Claim(JwtClaimTypes.Name, "Alice Smith"),
                new Claim(JwtClaimTypes.Email, "[email protected]")
            }
        },
        new TestUser { SubjectId = "11", Username = "bob", Password = "bob",
            Claims =
            {
                new Claim(JwtClaimTypes.Name, "Bob Smith"),
                new Claim(JwtClaimTypes.Email, "[email protected]")
            }
        }
    };
}

Point SPA to local Identity Server

 const config = {
    authority: 'http://localhost:5000/',
    client_id: 'spa',
    redirect_uri: 'http://localhost:8080/callback.html',
    response_type: 'code',
    scope: 'openid profile email api offline_access'
  };

Run SPA and test

npx http-server

identity-as-a-service's People

Contributors

karpikpl avatar

Stargazers

Jaroslav Beneš avatar Mohammed Ziad avatar Christopher Dooley avatar zeeshan asghar avatar Reginaldo Rubens da Silva avatar Oscar Torres avatar Damien Sawyer avatar Felipe Luís Pinheiro avatar  avatar Oscar Torres avatar shikelong avatar David Wei avatar Dave Beer avatar Ranjan Dailata avatar Fernando Groeler avatar  avatar Abdul Gaffur A Dama avatar Игорь Смирнов avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.