Skip to content

Commit

Permalink
The additional fixes of #231, #248
Browse files Browse the repository at this point in the history
  • Loading branch information
daisukenishino committed Sep 11, 2017
1 parent c15b04e commit 9a430ea
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using Touryo.Infrastructure.Framework.Presentation;
using Touryo.Infrastructure.Framework.Authentication;
using Touryo.Infrastructure.Framework.Exceptions;
using Touryo.Infrastructure.Public.Log;
using Touryo.Infrastructure.Public.Util;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

using Touryo.Infrastructure.Public.Security;

namespace Touryo.Infrastructure.Framework.Presentation
namespace Touryo.Infrastructure.Framework.Authentication
{
/// <summary>
/// OAuth2やOIDC関連のJwtToken処理
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
//**********************************************************************************
//* 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

//**********************************************************************************
//* クラス名 :OAuth2AndOIDCClient
//* クラス日本語名 :OAuth2AndOIDCClient(ライブラリ)
//*
//* 作成日時 :-
//* 作成者 :-
//* 更新履歴 :-
//*
//* 日時 更新者 内容
//* ---------- ---------------- -------------------------------------------------
//* 2017/04/24 西野 大介 新規
//**********************************************************************************

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;

using System.Web;

namespace Touryo.Infrastructure.Framework.Authentication
{
// ライブラリ内でawaitする場合は、ConfigureAwait(false)を使う。

/// <summary>OAuth2AndOIDCClient(ライブラリ)</summary>
public class OAuth2AndOIDCClient
{
/// <summary>HttpClient</summary>
private static HttpClient _HttpClient = null;

/// <summary>HttpClient</summary>
public static HttpClient HttpClient
{
set
{
OAuth2AndOIDCClient._HttpClient = value;
}
}

/// <summary>
/// Authorization Code Grant
/// 仲介コードからAccess Tokenを取得する。
/// </summary>
/// <param name="tokenEndpointUri">TokenエンドポイントのUri</param>
/// <param name="client_id">client_id</param>
/// <param name="client_secret">client_secret</param>
/// <param name="redirect_uri">redirect_uri</param>
/// <param name="code">仲介コード</param>
/// <returns>結果のJSON文字列</returns>
public static async Task<string> GetAccessTokenByCodeAsync(
Uri tokenEndpointUri, string client_id, string client_secret, string redirect_uri, string code)
{
// 4.1.3. アクセストークンリクエスト
// http://openid-foundation-japan.github.io/rfc6749.ja.html#token-req

// 通信用の変数
HttpRequestMessage httpRequestMessage = null;
HttpResponseMessage httpResponseMessage = null;

// HttpRequestMessage (Method & RequestUri)
httpRequestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = tokenEndpointUri,
};

// HttpRequestMessage (Headers & Content)

httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(
string.Format("{0}:{1}",
client_id, client_secret))));

httpRequestMessage.Content = new FormUrlEncodedContent(
new Dictionary<string, string>
{
{ "grant_type", "authorization_code" },
{ "code", code },
{ "redirect_uri", HttpUtility.HtmlEncode(redirect_uri) },
});

// HttpResponseMessage
httpResponseMessage = await OAuth2AndOIDCClient._HttpClient.SendAsync(httpRequestMessage).ConfigureAwait(false);
return await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
}

/// <summary>
/// Resource Owner Password Credentials Grant
/// </summary>
/// <param name="tokenEndpointUri">TokenエンドポイントのUri</param>
/// <param name="client_id">client_id</param>
/// <param name="client_secret">client_secret</param>
/// <param name="userId">userId</param>
/// <param name="password">password</param>
/// <param name="scopes">scopes</param>
/// <returns>結果のJSON文字列</returns>
public static async Task<string> GetAccessTokenByROPAsync(
Uri tokenEndpointUri, string client_id, string client_secret, string userId, string password, string scopes)
{
// 4.1.3. アクセストークンリクエスト
// http://openid-foundation-japan.github.io/rfc6749.ja.html#token-req

// 通信用の変数
HttpRequestMessage httpRequestMessage = null;
HttpResponseMessage httpResponseMessage = null;

// HttpRequestMessage (Method & RequestUri)
httpRequestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = tokenEndpointUri,
};

// HttpRequestMessage (Headers & Content)

httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(
string.Format("{0}:{1}",
client_id, client_secret))));

httpRequestMessage.Content = new FormUrlEncodedContent(
new Dictionary<string, string>
{
{ "grant_type", "password" },
{ "username", userId },
{ "password", password },
{ "scope", scopes },
});

// HttpResponseMessage
httpResponseMessage = await OAuth2AndOIDCClient._HttpClient.SendAsync(httpRequestMessage).ConfigureAwait(false);
return await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
}

/// <summary>Refresh Tokenを使用してAccess Tokenを更新</summary>
/// <param name="tokenEndpointUri">tokenEndpointUri</param>
/// <param name="refreshToken">refreshToken</param>
/// <returns>結果のJSON文字列</returns>
public static async Task<string> UpdateAccessTokenByRefreshTokenAsync(
Uri tokenEndpointUri, string refreshToken)
{
// 6. アクセストークンの更新
// http://openid-foundation-japan.github.io/rfc6749.ja.html#token-refresh

// 通信用の変数
HttpRequestMessage httpRequestMessage = null;
HttpResponseMessage httpResponseMessage = null;

// HttpRequestMessage (Method & RequestUri)
httpRequestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = tokenEndpointUri,
};

// HttpRequestMessage (Content)
httpRequestMessage.Content = new FormUrlEncodedContent(
new Dictionary<string, string>
{
{ "grant_type", "refresh_token" },
{ "refresh_token", refreshToken },
});

// HttpResponseMessage
httpResponseMessage = await OAuth2AndOIDCClient._HttpClient.SendAsync(httpRequestMessage).ConfigureAwait(false);
return await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
}

/// <summary>認可したユーザのClaim情報を取得するWebAPIを呼び出す</summary>
/// <param name="userInfoUri">Uri</param>
/// <param name="accessToken">accessToken</param>
/// <returns>結果のJSON文字列(認可したユーザのClaim情報)</returns>
public static async Task<string> CallUserInfoEndpointAsync(Uri userInfoUri, string accessToken)
{
// 通信用の変数
HttpRequestMessage httpRequestMessage = null;
HttpResponseMessage httpResponseMessage = null;

// HttpRequestMessage (Method & RequestUri)
httpRequestMessage = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = userInfoUri,
};

// HttpRequestMessage (Headers)
httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

// HttpResponseMessage
httpResponseMessage = await OAuth2AndOIDCClient._HttpClient.SendAsync(httpRequestMessage).ConfigureAwait(false);
return await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

using Touryo.Infrastructure.Public.Util;

namespace Touryo.Infrastructure.Framework.Presentation
namespace Touryo.Infrastructure.Framework.Authentication
{
/// <summary>OAuth2とOIDCの各種パラメタ</summary>
public class OAuth2AndOIDCParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Authentication\OAuth2AndOIDCClient.cs" />
<Compile Include="Business\BaseLogic.cs" />
<Compile Include="Business\TransactionControl.cs" />
<Compile Include="Common\BaseParameterValue.cs" />
Expand All @@ -111,8 +112,8 @@
</Compile>
<Compile Include="Presentation\BaseMVController.cs" />
<Compile Include="Presentation\FxEventArgs.cs" />
<Compile Include="Presentation\JwtToken.cs" />
<Compile Include="Presentation\OAuth2AndOIDCParams.cs" />
<Compile Include="Authentication\JwtToken.cs" />
<Compile Include="Authentication\OAuth2AndOIDCParams.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resources\FrameworkExceptionMessageResource.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Authentication\OAuth2ProviderHelper.cs" />
<Compile Include="Business\BaseLogic.cs" />
<Compile Include="Business\TransactionControl.cs" />
<Compile Include="Common\BaseParameterValue.cs" />
Expand All @@ -111,8 +112,8 @@
</Compile>
<Compile Include="Presentation\BaseMVController.cs" />
<Compile Include="Presentation\FxEventArgs.cs" />
<Compile Include="Presentation\JwtToken.cs" />
<Compile Include="Presentation\OAuth2AndOIDCParams.cs" />
<Compile Include="Authentication\JwtToken.cs" />
<Compile Include="Authentication\OAuth2AndOIDCParams.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resources\FrameworkExceptionMessageResource.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
<Compile Include="Dto\DTTables.cs" />
<Compile Include="Dto\DTTableStatus.cs" />
<Compile Include="Dto\DTType.cs" />
<Compile Include="IO\ASymmetricCryptography.cs" />
<Compile Include="IO\SymmetricCryptography.cs" />
<Compile Include="Security\ASymmetricCryptography.cs" />
<Compile Include="Security\SymmetricCryptography.cs" />
<Compile Include="IO\UnZipper.cs" />
<Compile Include="IO\ZipBase.cs" />
<Compile Include="IO\Zipper.cs" />
Expand All @@ -115,17 +115,17 @@
<Compile Include="Db\BaseDam.cs" />
<Compile Include="Db\DamSqlSvr.cs" />
<Compile Include="Db\DbEnum.cs" />
<Compile Include="Util\DigitalSignX509.cs" />
<Compile Include="Util\DigitalSignXML.cs" />
<Compile Include="Util\DigitalSign.cs" />
<Compile Include="Security\DigitalSignX509.cs" />
<Compile Include="Security\DigitalSignXML.cs" />
<Compile Include="Security\DigitalSign.cs" />
<Compile Include="Util\CustomMarshaler.cs" />
<Compile Include="Util\GetHash.cs" />
<Compile Include="Util\GetKeyedHash.cs" />
<Compile Include="Util\GetPassword.cs" />
<Compile Include="Util\IdentityImpersonation.cs" />
<Compile Include="Util\JWT_HS256.cs" />
<Compile Include="Util\JWT_RS256.cs" />
<Compile Include="Util\JWT.cs" />
<Compile Include="Security\GetHash.cs" />
<Compile Include="Security\GetKeyedHash.cs" />
<Compile Include="Security\GetPassword.cs" />
<Compile Include="Security\IdentityImpersonation.cs" />
<Compile Include="Security\JWT_HS256.cs" />
<Compile Include="Security\JWT_RS256.cs" />
<Compile Include="Security\JWT.cs" />
<Compile Include="Util\ObjectInspector.cs" />
<Compile Include="Util\PubCmnFunction.cs" />
<Compile Include="Util\PublicExceptionMessage.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@

using Newtonsoft.Json.Linq;

using Touryo.Infrastructure.Framework.Presentation;
using Touryo.Infrastructure.Framework.Transmission;
using Touryo.Infrastructure.Framework.Authentication;
using Touryo.Infrastructure.Framework.Exceptions;
using Touryo.Infrastructure.Framework.Common;
using Touryo.Infrastructure.Framework.Util;
using Touryo.Infrastructure.Framework.Transmission;

using Touryo.Infrastructure.Public.Db;
using Touryo.Infrastructure.Public.IO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

using Newtonsoft.Json.Linq;

using Touryo.Infrastructure.Framework.Presentation;
using Touryo.Infrastructure.Framework.Authentication;
using Touryo.Infrastructure.Framework.Exceptions;
using Touryo.Infrastructure.Framework.Common;
using Touryo.Infrastructure.Framework.Util;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@

using Newtonsoft.Json.Linq;

using Touryo.Infrastructure.Framework.Presentation;
using Touryo.Infrastructure.Framework.Transmission;
using Touryo.Infrastructure.Framework.Authentication;
using Touryo.Infrastructure.Framework.Exceptions;
using Touryo.Infrastructure.Framework.Common;
using Touryo.Infrastructure.Framework.Util;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

using Newtonsoft.Json.Linq;

using Touryo.Infrastructure.Framework.Presentation;
using Touryo.Infrastructure.Framework.Authentication;
using Touryo.Infrastructure.Framework.Exceptions;
using Touryo.Infrastructure.Framework.Common;
using Touryo.Infrastructure.Framework.Util;
Expand Down
Loading

0 comments on commit 9a430ea

Please sign in to comment.