Skip to content

Commit

Permalink
Now, I am corresponding to #77.
Browse files Browse the repository at this point in the history
  • Loading branch information
daisukenishino2 committed Jun 20, 2019
1 parent 9574aac commit cae74f9
Show file tree
Hide file tree
Showing 5 changed files with 381 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ CREATE TABLE [OAuth2Revocation](
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [IssuedToken](
[Jti] [nvarchar](38) NOT NULL, -- PK, guid
[Value] [nvarchar](max) NULL, -- IssuedToken
[ClientID] [nvarchar](38) NOT NULL,
[Audience] [nvarchar](38) NOT NULL,
[CreatedDate] [smalldatetime] NOT NULL,
CONSTRAINT [PK.IssuedToken] PRIMARY KEY NONCLUSTERED ([Jti] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [RequestObject](
[Urn] [nvarchar](38) NOT NULL, -- PK, guid
[Value] [nvarchar](max) NULL, -- RequestObject
[CreatedDate] [smalldatetime] NOT NULL,
CONSTRAINT [PK.RequestObject] PRIMARY KEY NONCLUSTERED ([Urn] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

-- INDEX
---- Users
Expand Down
180 changes: 180 additions & 0 deletions root/programs/CommonLibrary/Extensions/Sts/IssuedTokenProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
//**********************************************************************************
//* Copyright (C) 2017 Hitachi Solutions,Ltd.
//**********************************************************************************

#region Apache License
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

//**********************************************************************************
//* クラス名 :IssuedTokenProvider
//* クラス日本語名 :IssueしたOAuth2のTokenのjtiを保存する(ライブラリ)
//*
//* 作成日時 :-
//* 作成者 :-
//* 更新履歴 :-
//*
//* 日時 更新者 内容
//* ---------- ---------------- -------------------------------------------------
//* 2019/06/20 西野 大介 新規
//**********************************************************************************

using MultiPurposeAuthSite.Co;
using MultiPurposeAuthSite.Data;

using System;
using System.Data;
using System.Collections.Concurrent;

using Dapper;

namespace MultiPurposeAuthSite.Extensions.Sts
{
/// <summary>
/// IssueしたOAuth2のTokenのjtiを保存する。
/// </summary>
public class IssuedTokenProvider
{
/// <summary>
/// OAuth2RevocationProvider
/// ConcurrentDictionaryは、.NET 4.0の新しいスレッドセーフなHashtable
/// </summary>
private static ConcurrentDictionary<string, DateTime> IssuedTokens = new ConcurrentDictionary<string, DateTime>();

#region Create

/// <summary>Create</summary>
/// <param name="jti">string</param>
public static void Create(string jti)
{
switch (Config.UserStoreType)
{
case EnumUserStoreType.Memory:
IssuedTokenProvider.IssuedTokens.TryAdd(jti, DateTime.Now);
break;

case EnumUserStoreType.SqlServer:
case EnumUserStoreType.ODPManagedDriver:
case EnumUserStoreType.PostgreSQL: // DMBMS

using (IDbConnection cnn = DataAccess.CreateConnection())
{
cnn.Open();

switch (Config.UserStoreType)
{
case EnumUserStoreType.SqlServer:

cnn.Execute(
"INSERT INTO [OAuth2Revocation] ([Jti], [CreatedDate]) VALUES (@Jti, @CreatedDate)",
new { Jti = jti, CreatedDate = DateTime.Now });

break;

case EnumUserStoreType.ODPManagedDriver:

cnn.Execute(
"INSERT INTO \"OAuth2Revocation\" (\"Jti\", \"CreatedDate\") VALUES (:Jti, :CreatedDate)",
new { Jti = jti, CreatedDate = DateTime.Now });

break;

case EnumUserStoreType.PostgreSQL:

cnn.Execute(
"INSERT INTO \"oauth2revocation\" (\"jti\", \"createddate\") VALUES (@Jti, @CreatedDate)",
new { Jti = jti, CreatedDate = DateTime.Now });

break;
}
}

break;
}
}

#endregion

#region Get(Reference)

/// <summary>Get</summary>
/// <param name="jti">string</param>
/// <returns>DateTime?</returns>
public static DateTime? Get(string jti)
{
DateTime? datetime = null;

switch (Config.UserStoreType)
{
case EnumUserStoreType.Memory:

DateTime temp = DateTime.MinValue;
if (IssuedTokenProvider.IssuedTokens.TryGetValue(jti, out temp))
{
datetime = temp;
}

break;

case EnumUserStoreType.SqlServer:
case EnumUserStoreType.ODPManagedDriver:
case EnumUserStoreType.PostgreSQL: // DMBMS

using (IDbConnection cnn = DataAccess.CreateConnection())
{
cnn.Open();

switch (Config.UserStoreType)
{
case EnumUserStoreType.SqlServer:

datetime = cnn.ExecuteScalar<DateTime>(
"SELECT [CreatedDate] FROM [OAuth2Revocation] WHERE [Jti] = @Jti", new { Jti = jti });

break;

case EnumUserStoreType.ODPManagedDriver:

datetime = cnn.ExecuteScalar<DateTime>(
"SELECT \"CreatedDate\" FROM \"OAuth2Revocation\" WHERE \"Jti\" = :Jti", new { Jti = jti });

break;

case EnumUserStoreType.PostgreSQL:

datetime = cnn.ExecuteScalar<DateTime>(
"SELECT \"createddate\" FROM \"oauth2revocation\" WHERE \"jti\" = @Jti", new { Jti = jti });

break;
}
}

break;
}

// {0001/01/01 00:00} チェック
if (datetime == DateTime.MinValue)
{
return null;
}
else
{
return datetime;
}
}

#endregion
}
}
180 changes: 180 additions & 0 deletions root/programs/CommonLibrary/Extensions/Sts/RequestObjectProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
//**********************************************************************************
//* Copyright (C) 2017 Hitachi Solutions,Ltd.
//**********************************************************************************

#region Apache License
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

//**********************************************************************************
//* クラス名 :RequestObjectProvider
//* クラス日本語名 :登録されたRequestObjectを管理する(ライブラリ)
//*
//* 作成日時 :-
//* 作成者 :-
//* 更新履歴 :-
//*
//* 日時 更新者 内容
//* ---------- ---------------- -------------------------------------------------
//* 2019/06/20 西野 大介 新規
//**********************************************************************************

using MultiPurposeAuthSite.Co;
using MultiPurposeAuthSite.Data;

using System;
using System.Data;
using System.Collections.Concurrent;

using Dapper;

namespace MultiPurposeAuthSite.Extensions.Sts
{
/// <summary>
/// 登録されたRequestObjectを管理する。
/// </summary>
public class RequestObjectProvider
{
/// <summary>
/// RequestObjects
/// ConcurrentDictionaryは、.NET 4.0の新しいスレッドセーフなHashtable
/// </summary>
private static ConcurrentDictionary<string, DateTime> RequestObjects = new ConcurrentDictionary<string, DateTime>();

#region Create

/// <summary>Create</summary>
/// <param name="jti">string</param>
public static void Create(string jti)
{
switch (Config.UserStoreType)
{
case EnumUserStoreType.Memory:
RequestObjectProvider.RequestObjects.TryAdd(jti, DateTime.Now);
break;

case EnumUserStoreType.SqlServer:
case EnumUserStoreType.ODPManagedDriver:
case EnumUserStoreType.PostgreSQL: // DMBMS

using (IDbConnection cnn = DataAccess.CreateConnection())
{
cnn.Open();

switch (Config.UserStoreType)
{
case EnumUserStoreType.SqlServer:

cnn.Execute(
"INSERT INTO [OAuth2Revocation] ([Jti], [CreatedDate]) VALUES (@Jti, @CreatedDate)",
new { Jti = jti, CreatedDate = DateTime.Now });

break;

case EnumUserStoreType.ODPManagedDriver:

cnn.Execute(
"INSERT INTO \"OAuth2Revocation\" (\"Jti\", \"CreatedDate\") VALUES (:Jti, :CreatedDate)",
new { Jti = jti, CreatedDate = DateTime.Now });

break;

case EnumUserStoreType.PostgreSQL:

cnn.Execute(
"INSERT INTO \"oauth2revocation\" (\"jti\", \"createddate\") VALUES (@Jti, @CreatedDate)",
new { Jti = jti, CreatedDate = DateTime.Now });

break;
}
}

break;
}
}

#endregion

#region Get(Reference)

/// <summary>Get</summary>
/// <param name="jti">string</param>
/// <returns>DateTime?</returns>
public static DateTime? Get(string jti)
{
DateTime? datetime = null;

switch (Config.UserStoreType)
{
case EnumUserStoreType.Memory:

DateTime temp = DateTime.MinValue;
if (RequestObjectProvider.RequestObjects.TryGetValue(jti, out temp))
{
datetime = temp;
}

break;

case EnumUserStoreType.SqlServer:
case EnumUserStoreType.ODPManagedDriver:
case EnumUserStoreType.PostgreSQL: // DMBMS

using (IDbConnection cnn = DataAccess.CreateConnection())
{
cnn.Open();

switch (Config.UserStoreType)
{
case EnumUserStoreType.SqlServer:

datetime = cnn.ExecuteScalar<DateTime>(
"SELECT [CreatedDate] FROM [OAuth2Revocation] WHERE [Jti] = @Jti", new { Jti = jti });

break;

case EnumUserStoreType.ODPManagedDriver:

datetime = cnn.ExecuteScalar<DateTime>(
"SELECT \"CreatedDate\" FROM \"OAuth2Revocation\" WHERE \"Jti\" = :Jti", new { Jti = jti });

break;

case EnumUserStoreType.PostgreSQL:

datetime = cnn.ExecuteScalar<DateTime>(
"SELECT \"createddate\" FROM \"oauth2revocation\" WHERE \"jti\" = @Jti", new { Jti = jti });

break;
}
}

break;
}

// {0001/01/01 00:00} チェック
if (datetime == DateTime.MinValue)
{
return null;
}
else
{
return datetime;
}
}

#endregion
}
}
Loading

0 comments on commit cae74f9

Please sign in to comment.