Example #1
0
        /// <summary>constructor</summary>
        public JwkSetStore()
        {
            if (string.IsNullOrEmpty(OAuth2AndOIDCParams.JwkSetUri))
            {
                this._jwkSet = new JwkSet();
            }
            else
            {
                // _jwkSet 更新
                this._jwkSet = JsonConvert.DeserializeObject <JwkSet>(
                    OAuth2AndOIDCClient.GetJwkSetAsync(
                        new Uri(OAuth2AndOIDCParams.JwkSetUri)).Result);

                // _dateTime 更新
                this._dateTime = DateTime.Now;

                if (this._jwkSet.keys.Count == 0)
                {
                    Debug.WriteLine("JwkSet was abnormally initarized with an empty state in JwkSetStore constructor.");
                }
                else
                {
                    Debug.WriteLine("JwkSet was initarized normally in JwkSetStore constructor.");
                }
            }
        }
Example #2
0
        /// <summary>SetJwkSetObjectAsync</summary>
        /// <param name="jku">string</param>
        /// <param name="kid">string</param>
        /// <returns>JwkObject</returns>
        public JObject SetJwkSetObject(string jku, string kid)
        {
            if (jku != OAuth2AndOIDCParams.JwkSetUri)
            {
                // 一致しなかった場合、以下の処理を施しリトライ。
                if (jku.EndsWith("/"))
                {
                    jku = jku.Substring(0, jku.Length - 1);
                }
                else
                {
                    jku = jku + "/";
                }

                if (jku != OAuth2AndOIDCParams.JwkSetUri)
                {
                    return(null); // 上位で証明書利用へ遷移
                }
            }

            try
            {
                // ライターロックを取得
                this._rwLock.AcquireWriterLock(Timeout.Infinite);

                #region 書込

                TimeSpan timeSpan = DateTime.Now.Subtract(this._dateTime);

                if (timeSpan.TotalSeconds < OAuth2AndOIDCParams.JwkSetUpdateIntervalInSeconds)
                {
                    // x秒(既定10秒)以内に更新済み ≒ 更新済みと判断。
                }
                else
                {
                    // x秒(既定10秒)以内に更新済みでない
                    // ≒ 鍵変更後、更新済みでないと判断。

                    // JwkSetUri
                    string jwkSetString = OAuth2AndOIDCClient.GetJwkSetAsync(new Uri(jku)).Result;

                    if (string.IsNullOrEmpty(jwkSetString))
                    {
                        // jwkSetStringが空文字列
                        Debug.WriteLine("JwkSet was not updated, because jwkSetString is null or empty in JwkSetStore.SetJwkSetObject method.");
                    }
                    else
                    {
                        JwkSet jwkSet = JsonConvert.DeserializeObject <JwkSet>(jwkSetString);

                        // _jwkSet 更新
                        this._jwkSet = jwkSet;
                        // _dateTime 更新
                        this._dateTime = DateTime.Now;

                        Debug.WriteLine("JwkSet was updated normally in JwkSetStore.SetJwkSetObject method.");
                    }
                }

                #endregion
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception was catched in JwkSetStore.SetJwkSetObject method: " + ex.ToString());
            }
            finally
            {
                // ライターロックを解放
                this._rwLock.ReleaseWriterLock();
            }

            // JwkSetからJwkを返す。
            return(JwkSet.GetJwkObject(this._jwkSet, kid));
        }