public AuthenticateResult Authenticate(Server server, Connection connection, UserInfo userInfo) { // ニックネームとパスワードのチェック if (String.IsNullOrEmpty(userInfo.Nick)) { return new AuthenticateResult(ErrorReply.ERR_NONICKNAMEGIVEN, "No nickname given"); } if (String.IsNullOrEmpty(userInfo.Password)) { return new AuthenticateResult(ErrorReply.ERR_PASSWDMISMATCH, "Password Incorrect"); } // ログインチェック // この段階でTwitterServiceは作っておく connection.SendGatewayServerMessage("* アカウント認証を確認しています..."); TwitterService twitter = new TwitterService(userInfo.UserName, userInfo.Password); User twitterUser = null; try { twitterUser = twitter.VerifyCredential(); } catch (WebException we) { // Twitter の接続に失敗 connection.SendGatewayServerMessage("* アカウント認証に失敗しました。ユーザ名またはパスワードを確認してください。"); return new AuthenticateResult(ErrorReply.ERR_PASSWDMISMATCH, "Password Incorrect"); } catch (Exception ex) { // Twitter の接続に失敗 connection.SendGatewayServerMessage("* アカウント認証に失敗しました。ユーザ名またはパスワードを確認してください。内部的なエラーが発生しました。"); Trace.TraceError(ex.ToString()); return new AuthenticateResult(ErrorReply.ERR_PASSWDMISMATCH, "Password Incorrect"); } connection.SendGatewayServerMessage(String.Format("* アカウント: {0} (ID:{1})", twitterUser.ScreenName, twitterUser.Id)); return new TwitterAuthenticateResult(twitterUser); // 成功 }
public AuthenticateResult Authenticate(Server server, Connection connection, UserInfo userInfo) { // ニックネームとパスワードのチェック if (String.IsNullOrEmpty(userInfo.Nick)) { return new AuthenticateResult(ErrorReply.ERR_NONICKNAMEGIVEN, "No nickname given"); } // OAuth ログイン/設定 if (String.IsNullOrEmpty(userInfo.Password)) { connection.SendGatewayServerMessage("* OAuth 認証の設定を開始します..."); return new OAuthContinueAuthenticationResult(); } else { var config = Config.LoadConfig(userInfo.UserName); // HOSTING => 番号的 ID になる // xAuth fallback if (true && String.IsNullOrEmpty(config.OAuthUserPasswordHash)) { return new XAuthAuthentication().Authenticate(server, connection, userInfo); } connection.SendGatewayServerMessage("* アカウント認証を確認しています(OAuth)..."); // OAuth 設定未設定 if (String.IsNullOrEmpty(config.OAuthAccessToken) || String.IsNullOrEmpty(config.OAuthTokenSecret)) { connection.SendGatewayServerMessage("* OAuth 認証の設定を開始します..."); return new OAuthContinueAuthenticationResult(); } // 設定してあるパスワードとの照合 if (Utility.GetMesssageDigest(userInfo.Password) != config.OAuthUserPasswordHash) { connection.SendGatewayServerMessage("* アカウント認証に失敗しました。ユーザ名またはパスワードを確認してください。"); return new AuthenticateResult(ErrorReply.ERR_PASSWDMISMATCH, "Password Incorrect"); } // ユーザー認証問い合わせをしてみる try { TwitterOAuth twitterOAuth = new TwitterOAuth(server.OAuthClientKey, server.OAuthSecretKey) { Token = config.OAuthAccessToken, TokenSecret = config.OAuthTokenSecret }; TwitterIdentity identity = new TwitterIdentity { Token = config.OAuthAccessToken, TokenSecret = config.OAuthTokenSecret }; TwitterService twitterService = new TwitterService(server.OAuthClientKey, server.OAuthSecretKey, identity); User twitterUser = twitterService.VerifyCredential(); identity.ScreenName = twitterUser.ScreenName; identity.UserId = twitterUser.Id; connection.SendGatewayServerMessage(String.Format("* アカウント: {0} (ID:{1})", twitterUser.ScreenName, twitterUser.Id)); return new TwitterAuthenticateResult(twitterUser, identity); } catch (Exception ex) { connection.SendServerErrorMessage(TwitterOAuth.GetMessageFromException(ex)); return new AuthenticateResult(ErrorReply.ERR_PASSWDMISMATCH, "Password Incorrect"); } } }