示例#1
0
        // AuthZに実装(パラメタ体系が違うため)
        #endregion

        #region Verify

        /// <summary>汎用認証サイトの発行したIdTokenを検証する。</summary>
        /// <param name="id_token">string</param>
        /// <param name="access_token">string</param>
        /// <param name="code">string</param>
        /// <param name="state">string</param>
        /// <param name="sub">out string</param>
        /// <param name="nonce">out string</param>
        /// <param name="jobj">out JObject</param>
        /// <returns>検証結果</returns>
        public static bool Verify(string id_token,
                                  string access_token, string code, string state,
                                  out string sub, out string nonce, out JObject jobj)
        {
            sub   = "";
            nonce = "";
            jobj  = null;

            // JWS検証
            string jwtPayload = "";

            if (CmnJwtToken.Verify(id_token, out jwtPayload))
            {
                jobj = ((JObject)JsonConvert.DeserializeObject(jwtPayload));

                #region クレーム検証

                // out
                sub   = (string)jobj[OAuth2AndOIDCConst.sub];
                nonce = (string)jobj[OAuth2AndOIDCConst.nonce];

                string iss = (string)jobj[OAuth2AndOIDCConst.iss];
                string aud = (string)jobj[OAuth2AndOIDCConst.aud];
                //string iat = (string)jobj[OAuth2AndOIDCConst.iat];
                string exp = (string)jobj[OAuth2AndOIDCConst.exp];

                #region ハッシュ・クレーム検証

                // at_hash
                string at_hash = (string)jobj[OAuth2AndOIDCConst.at_hash];
                if (!string.IsNullOrEmpty(access_token) && !string.IsNullOrEmpty(at_hash))
                {
                    if (!IdToken.VerifyHash(access_token, at_hash))
                    {
                        return(false);
                    }
                }

                // c_hash
                string c_hash = (string)jobj[OAuth2AndOIDCConst.c_hash];
                if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(c_hash))
                {
                    if (!IdToken.VerifyHash(code, c_hash))
                    {
                        return(false);
                    }
                }

                // s_hash
                string s_hash = (string)jobj[OAuth2AndOIDCConst.s_hash];
                if (!string.IsNullOrEmpty(state) && !string.IsNullOrEmpty(s_hash))
                {
                    if (!IdToken.VerifyHash(state, s_hash))
                    {
                        return(false);
                    }
                }

                #endregion

                long unixTimeSeconds = 0;

#if NET45
                unixTimeSeconds = PubCmnFunction.ToUnixTime(DateTimeOffset.Now);
#else
                unixTimeSeconds = DateTimeOffset.Now.ToUnixTimeSeconds();
#endif

                if (iss == CmnClientParams.Isser &&
                    long.Parse(exp) >= unixTimeSeconds)
                {
                    if (string.IsNullOrEmpty(OAuth2AndOIDCParams.JwkSetFilePath))
                    {
                        // Client側
                        if (aud == OAuth2AndOIDCParams.ClientID)
                        {
                            // OAuth2 Clientバージョンの実装で成功
                            return(true);
                        }
                        else if (OAuth2AndOIDCParams.ClientIDs.Any(x => x == aud))
                        {
                            // OAuth2 ResourcesServerバージョンの実装で成功
                            return(true);
                        }
                        else
                        {
                            // JWTの内容検証に失敗
                        }
                    }
                    else
                    {
                        // AuthZ側(検証用カバレッジ
                        // OAuth2 AuthZバージョンの実装で成功
                        return(true);
                    }
                }
                else
                {
                    // JWTの内容検証に失敗
                }

                #endregion
            }
            else
            {
                // JWTの署名検証に失敗
            }

            // 認証に失敗
            return(false);
        }
        // AuthZに実装(パラメタ体系が違うため)
        #endregion

        #region Verify
        /// <summary>汎用認証サイトの発行したResponseObjectを検証する。</summary>
        /// <param name="response">string</param>
        /// <param name="jobj">out JObject</param>
        /// <returns>検証結果</returns>
        public static bool Verify(string response, out JObject jobj)
        {
            // JWS検証
            string jwtPayload = "";

            if (CmnJwtToken.Verify(response, out jwtPayload))
            {
                jobj = ((JObject)JsonConvert.DeserializeObject(jwtPayload));

                #region クレーム検証

                if (jobj.ContainsKey(OAuth2AndOIDCConst.error))
                {
                    // errorの場合
                    return(true);
                }
                else
                {
                    // errorでない場合

                    string iss = (string)jobj[OAuth2AndOIDCConst.iss];
                    string aud = (string)jobj[OAuth2AndOIDCConst.aud];
                    //string iat = (string)jobj[OAuth2AndOIDCConst.iat];
                    string exp = (string)jobj[OAuth2AndOIDCConst.exp];

                    long unixTimeSeconds = 0;
#if NET45
                    unixTimeSeconds = PubCmnFunction.ToUnixTime(DateTimeOffset.Now);
#else
                    unixTimeSeconds = DateTimeOffset.Now.ToUnixTimeSeconds();
#endif

                    if (iss == CmnClientParams.Isser &&
                        long.Parse(exp) >= unixTimeSeconds)
                    {
                        if (string.IsNullOrEmpty(OAuth2AndOIDCParams.JwkSetFilePath))
                        {
                            // Client側
                            if (aud == OAuth2AndOIDCParams.ClientID)
                            {
                                // OAuth2 Clientバージョンの実装で成功
                                return(true);
                            }
                            else if (OAuth2AndOIDCParams.ClientIDs.Any(x => x == aud))
                            {
                                // OAuth2 ResourcesServerバージョンの実装で成功
                                return(true);
                            }
                            else
                            {
                                // JWTの内容検証に失敗
                            }
                        }
                        else
                        {
                            // AuthZ側(検証用カバレッジ
                            // OAuth2 AuthZバージョンの実装で成功
                            return(true);
                        }
                    }
                    else
                    {
                        // JWTの内容検証に失敗
                    }
                }

                #endregion
            }
            else
            {
                // JWTの署名検証に失敗
                jobj = null;
            }

            // 認証に失敗
            return(false);
        }
示例#3
0
        // AuthZに実装(パラメタ体系が違うため)
        #endregion

        #region Verify
        /// <summary>汎用認証サイトの発行したAccessTokenを検証する。</summary>
        /// <param name="access_token">
        /// AccessTokenで以下の項目が必要
        ///  - iss
        ///  - aud
        ///  - iat
        ///  - exp
        ///  - sub
        ///  - roles  (option)
        ///  - scopes (option)
        ///  - その他 (option)
        /// </param>
        /// <param name="sub">out string</param>
        /// <param name="roles">out List(string)</param>
        /// <param name="scopes">out List(string)</param>
        /// <param name="jobj">out JObject</param>
        /// <returns>検証結果</returns>
        public static bool Verify(string access_token,
                                  out string sub, out List <string> roles, out List <string> scopes, out JObject jobj)
        {
            sub    = "";
            roles  = new List <string>();
            scopes = new List <string>();
            jobj   = null;

            // JWS検証
            string jwtPayload = "";

            if (CmnJwtToken.Verify(access_token, out jwtPayload))
            {
                jobj = ((JObject)JsonConvert.DeserializeObject(jwtPayload));

                #region クレーム検証

                string iss = (string)jobj[OAuth2AndOIDCConst.iss];
                string aud = (string)jobj[OAuth2AndOIDCConst.aud];
                //string iat = (string)jobj[OAuth2AndOIDCConst.iat];
                string exp = (string)jobj[OAuth2AndOIDCConst.exp];

                sub = (string)jobj[OAuth2AndOIDCConst.sub];

                if (jobj[OAuth2AndOIDCConst.Scope_Roles] != null)
                {
                    roles = JsonConvert.DeserializeObject <List <string> >(jobj[OAuth2AndOIDCConst.Scope_Roles].ToString());
                }
                if (jobj[OAuth2AndOIDCConst.scopes] != null)
                {
                    scopes = JsonConvert.DeserializeObject <List <string> >(jobj[OAuth2AndOIDCConst.scopes].ToString());
                }

                long unixTimeSeconds = 0;
#if NET45
                unixTimeSeconds = PubCmnFunction.ToUnixTime(DateTimeOffset.Now);
#else
                unixTimeSeconds = DateTimeOffset.Now.ToUnixTimeSeconds();
#endif

                if (iss == CmnClientParams.Isser &&
                    long.Parse(exp) >= unixTimeSeconds)
                {
                    if (string.IsNullOrEmpty(OAuth2AndOIDCParams.JwkSetFilePath))
                    {
                        // Client側
                        if (aud == OAuth2AndOIDCParams.ClientID)
                        {
                            // OAuth2 Clientバージョンの実装で成功
                            return(true);
                        }
                        else if (OAuth2AndOIDCParams.ClientIDs.Any(x => x == aud))
                        {
                            // OAuth2 ResourcesServerバージョンの実装で成功
                            return(true);
                        }
                        else
                        {
                            // JWTの内容検証に失敗
                        }
                    }
                    else
                    {
                        // AuthZ側(検証用カバレッジ
                        // OAuth2 AuthZバージョンの実装で成功
                        return(true);
                    }
                }
                else
                {
                    // JWTの内容検証に失敗
                }

                #endregion
            }
            else
            {
                // JWTの署名検証に失敗
            }

            // 認証に失敗
            return(false);
        }