Skip to content

Commit

Permalink
Create RefreshToken and Client Table
Browse files Browse the repository at this point in the history
Create class SimpleAuthorizationServerProvider and SimpleAuthorizationServerProvider
Add Oauthentication Refresh Token in Api
  • Loading branch information
lcorozco10 committed Jan 27, 2016
1 parent 27110d8 commit 7441651
Show file tree
Hide file tree
Showing 16 changed files with 787 additions and 10 deletions.
21 changes: 20 additions & 1 deletion Api.Rest.Secure/Api.Rest.Secure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,31 @@
<Compile Include="AuthRepository.cs" />
<Compile Include="Controller\AccountController.cs" />
<Compile Include="Controller\OrdersController.cs" />
<Compile Include="Migrations\201601262034249_InitialCreate.cs" />
<Compile Include="Migrations\201601262034249_InitialCreate.Designer.cs">
<DependentUpon>201601262034249_InitialCreate.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\201601272316354_Add_Client_And_RefreshToken_Tables.cs" />
<Compile Include="Migrations\201601272316354_Add_Client_And_RefreshToken_Tables.Designer.cs">
<DependentUpon>201601272316354_Add_Client_And_RefreshToken_Tables.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\Configuration.cs" />
<Compile Include="Models\Entities\Client.cs" />
<Compile Include="Models\Entities\RefreshToken.cs" />
<Compile Include="Models\UserModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\SimpleAuthorizationServerProvider.cs" />
<Compile Include="Providers\SimpleRefreshTokenProvider.cs" />
<Compile Include="StartUp.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<EmbeddedResource Include="Migrations\201601262034249_InitialCreate.resx">
<DependentUpon>201601262034249_InitialCreate.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Migrations\201601272316354_Add_Client_And_RefreshToken_Tables.resx">
<DependentUpon>201601272316354_Add_Client_And_RefreshToken_Tables.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down
5 changes: 5 additions & 0 deletions Api.Rest.Secure/AuthContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using Api.Rest.Secure.Models.Entities;

namespace Api.Rest.Secure
{
Expand All @@ -13,5 +15,8 @@ public AuthContext()
{

}

public DbSet<Client> Clients { get; set; }
public DbSet<RefreshToken> RefreshTokens { get; set; }
}
}
60 changes: 58 additions & 2 deletions Api.Rest.Secure/AuthRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Api.Rest.Secure.Models.Entities;

namespace Api.Rest.Secure
{
public class AuthRepository : IDisposable
{
private AuthContext _ctx;
private readonly AuthContext _ctx;

private UserManager<IdentityUser> _userManager;
private readonly UserManager<IdentityUser> _userManager;

public AuthRepository()
{
Expand Down Expand Up @@ -46,5 +47,60 @@ public void Dispose()
_userManager.Dispose();

}


public Client FindClient(string clientId)
{
var client = _ctx.Clients.Find(clientId);

return client;
}

public async Task<bool> AddRefreshToken(RefreshToken token)
{

var existingToken = _ctx.RefreshTokens.SingleOrDefault(r => r.Subject == token.Subject && r.ClientId == token.ClientId);

if (existingToken != null)
{
await RemoveRefreshToken(existingToken);
}

_ctx.RefreshTokens.Add(token);

return await _ctx.SaveChangesAsync() > 0;
}

public async Task<bool> RemoveRefreshToken(string refreshTokenId)
{
var refreshToken = await _ctx.RefreshTokens.FindAsync(refreshTokenId);

if (refreshToken != null)
{
_ctx.RefreshTokens.Remove(refreshToken);
return await _ctx.SaveChangesAsync() > 0;
}

return false;
}

public async Task<bool> RemoveRefreshToken(RefreshToken refreshToken)
{
_ctx.RefreshTokens.Remove(refreshToken);
return await _ctx.SaveChangesAsync() > 0;
}

public async Task<RefreshToken> FindRefreshToken(string refreshTokenId)
{
var refreshToken = await _ctx.RefreshTokens.FindAsync(refreshTokenId);

return refreshToken;
}

public List<RefreshToken> GetAllRefreshTokens()
{
return _ctx.RefreshTokens.ToList();
}

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions Api.Rest.Secure/Migrations/201601262034249_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
namespace Api.Rest.Secure.Migrations
{
using System;
using System.Data.Entity.Migrations;

public partial class InitialCreate : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.AspNetRoles",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");

CreateTable(
"dbo.AspNetUserRoles",
c => new
{
UserId = c.String(nullable: false, maxLength: 128),
RoleId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId)
.Index(t => t.RoleId);

CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");

CreateTable(
"dbo.AspNetUserClaims",
c => new
{
Id = c.Int(nullable: false, identity: true),
UserId = c.String(nullable: false, maxLength: 128),
ClaimType = c.String(),
ClaimValue = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);

CreateTable(
"dbo.AspNetUserLogins",
c => new
{
LoginProvider = c.String(nullable: false, maxLength: 128),
ProviderKey = c.String(nullable: false, maxLength: 128),
UserId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);

}

public override void Down()
{
DropForeignKey("dbo.AspNetUserRoles", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.AspNetUserLogins", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.AspNetUserClaims", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles");
DropIndex("dbo.AspNetUserLogins", new[] { "UserId" });
DropIndex("dbo.AspNetUserClaims", new[] { "UserId" });
DropIndex("dbo.AspNetUsers", "UserNameIndex");
DropIndex("dbo.AspNetUserRoles", new[] { "RoleId" });
DropIndex("dbo.AspNetUserRoles", new[] { "UserId" });
DropIndex("dbo.AspNetRoles", "RoleNameIndex");
DropTable("dbo.AspNetUserLogins");
DropTable("dbo.AspNetUserClaims");
DropTable("dbo.AspNetUsers");
DropTable("dbo.AspNetUserRoles");
DropTable("dbo.AspNetRoles");
}
}
}
126 changes: 126 additions & 0 deletions Api.Rest.Secure/Migrations/201601262034249_InitialCreate.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Target" xml:space="preserve">
<value>H4sIAAAAAAAEAO1dW2/rNhJ+X2D/g+Cn7iK1c9kcnA2cFqmT7HqbG+Kcom8HtEQ73CNRqkTlJCj2l+1Df1L/wpISJZEiKVOyfEmxKFDEvHwzHM6QQ3JG5/f//jb+/jXwnRcY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2zgtknwe2Tfu16e9yFdllvjztPbFOj6Otzyp8l5ecwc+Za/l5IT+zzkM567iGW+W4t0tpWZrXpCNXy4ixTuHIlVMjJ1Tpy+Udm9HkZJmKV1hsJVk3MRM0JIU2EV+bSNRNsR4z7Jo0UeZtmsoZEqSbafNtqpM3bNNM2pCLtIm1Pmxiky6NcsRQbYji0GZl7mKZnHIC1GCTFNIRFvYOsvPUFIVmJIahn75Pw1hdDn2bRIulOjcmhu7vwj75QDyNBywqC/RMwGLrSvl62meJFWLgXNY6KJrX7oVtIgEc3/YuYoAVwCa1mN9zZt6/4B5augjn0pvg+JVFK6JBhMPel6zbmpjTRzzILZZ7H91H29cY+hkDZROxl4B7/kCLfK/m+1txIGSCY/8Pvk9lcEnavvHwrke5CbAnExVe6bU8wiHwKltzjGWAxKe15o+p3A5fAfavuH00gqydCFvv4EoFlDIKEY1T96U+qw17w+t3/AEZdLej7aAAA</value>
</data>
<data name="DefaultSchema" xml:space="preserve">
<value>dbo</value>
</data>
</root>
Loading

0 comments on commit 7441651

Please sign in to comment.