示例#1
0
 def test_Create_OAuthAuthentication_TwoFactorAuthentication_TokenApi(self):
     db = Database()
     db.Initialize()
     username = '******'  # 存在するユーザ名。Token登録済み。TwoFactorSecretあり。
     #        token = 'dummy00000000'
     creator = AuthenticationsCreator(db, username)
     authentications = creator.Create(
     )  # [OAuthAuthentication, BasicAuthentication]
     self.assertEqual(list, type(authentications))
     self.assertEqual(2, len(authentications))
     self.assertEqual(OAuthAuthentication, type(authentications[0]))
     self.assertEqual(TwoFactorAuthentication, type(authentications[1]))
     reqp = RequestParameter(db, authentications)
     # Token認証API
     http_method = 'GET'
     #        endpoint = 'https://api.github.com/user'
     endpoint = 'user'
     params = reqp.Get(http_method, endpoint)
     print(params)
     self.assertTrue('headers' in params)
     self.assertTrue('Time-Zone' in params['headers'])
     self.assertTrue('Asia/Tokyo' in params['headers']['Time-Zone'])
     self.assertTrue('User-Agent' in params['headers'])
     self.assertEqual('', params['headers']['User-Agent'])
     self.assertTrue('Accept' in params['headers'])
     self.assertEqual('application/vnd.github.v3+json',
                      params['headers']['Accept'])
     self.assertTrue('Authorization' in params['headers'])
示例#2
0
 def test_Create_UnregisteredException_ConstractorParameter(self):
     db = Database()
     db.Initialize()
     username = '******'  # 存在しないユーザ名
     creator = AuthenticationsCreator(db, username)
     with self.assertRaises(Exception) as e:
         creator.Create()
         self.assertEqual(
             e.msg,
             '指定したユーザ {0} はDBに未登録です。登録してから実行してください。'.format(username))
示例#3
0
 def test_Create_OAuthAuthentication_TwoFactorAuthentication(self):
     db = Database()
     db.Initialize()
     username = '******'  # 存在するユーザ名。Token登録済み。TwoFactorSecretあり。
     creator = AuthenticationsCreator(db, username)
     authentications = creator.Create(
     )  # [OAuthAuthentication, TwoFactorAuthentication]
     self.assertEqual(list, type(authentications))
     self.assertEqual(2, len(authentications))
     self.assertEqual(OAuthAuthentication, type(authentications[0]))
     self.assertEqual(TwoFactorAuthentication, type(authentications[1]))
示例#4
0
 def test_Create_OAuthAuthentication_BasicAuthentication(self):
     db = Database()
     db.Initialize()
     username = '******'  # 存在するユーザ名。Token登録済み。TwoFactorSecretなし。
     creator = AuthenticationsCreator(db, username)
     authentications = creator.Create(
     )  # [OAuthAuthentication, BasicAuthentication]
     self.assertEqual(list, type(authentications))
     self.assertEqual(2, len(authentications))
     self.assertEqual(OAuthAuthentication, type(authentications[0]))
     self.assertEqual(BasicAuthentication, type(authentications[1]))
示例#5
0
 def test_Create_OAuthAuthentication_BasicAuthentication_BasicApi(self):
     db = Database()
     db.Initialize()
     username = '******'  # 存在するユーザ名。Token登録済み。TwoFactorSecretなし。
     token = 'TestToken000'
     creator = AuthenticationsCreator(db, username)
     authentications = creator.Create(
     )  # [OAuthAuthentication, BasicAuthentication]
     self.assertEqual(list, type(authentications))
     self.assertEqual(2, len(authentications))
     self.assertEqual(OAuthAuthentication, type(authentications[0]))
     self.assertEqual(BasicAuthentication, type(authentications[1]))
     reqp = RequestParameter(db, authentications)
     # Basic認証のみ使えるAPI
     http_method = 'POST'
     #        endpoint = 'https://api.github.com/authorizations'
     endpoint = 'authorizations'
     params = reqp.Get(http_method, endpoint)
     print(params)
     self.assertTrue('headers' in params)
     self.assertTrue('Time-Zone' in params['headers'])
     self.assertTrue('Asia/Tokyo' in params['headers']['Time-Zone'])
     self.assertTrue('User-Agent' in params['headers'])
     self.assertEqual('', params['headers']['User-Agent'])
     self.assertTrue('Accept' in params['headers'])
     self.assertEqual('application/vnd.github.v3+json',
                      params['headers']['Accept'])
     #        self.assertTrue('Authorization' in params['headers'])
     #        self.assertEqual('token ' + token, params['headers']['Authorization'])
     self.assertTrue('auth' in params)
     self.assertTrue(tuple, type(params['auth']))
     self.assertTrue(2, len(params['auth']))
     self.assertTrue(username, params['auth'][0])
     db_account = dataset.connect(
         'sqlite:///' + './database/res/db/GitHub.Accounts.sqlite3')
     password = db_account['Accounts'].find_one(
         Username=username)['Password']
     self.assertTrue(username, params['auth'][0])
     self.assertTrue(password, params['auth'][1])
示例#6
0
class GitHubOtpCreator:
    def __init__(self):
        self.__path_dir_this = os.path.abspath(os.path.dirname(__file__))
        self.__db = Database(self.__path_dir_this)
        #        self.__db = Database()
        self.__db.Initialize()

    def Create(self):
        self.__totp = pyotp.TOTP(self.__GetUserSecret())
        #        print("otp = {0}".format(self.__totp.now()))
        web.log.Log.Log().Logger.info("otp = {0}".format(self.__totp.now()))
        pyperclip.copy(self.__totp.now())

    def __GetUserSecret(self):
        parser = argparse.ArgumentParser(
            description='GitHub Repository Uploader.', )
        parser.add_argument('-u', '--username', '--user')
        args = parser.parse_args()

        username = args.username
        if None is username:
            config = configparser.ConfigParser()
            config.read('./config.ini')
            if not ('GitHub' in config):
                raise Exception(
                    'ユーザ名が必要です。しかし起動引数にもconfig.iniにも存在しません。起動引数なら第一引数にユーザ名を渡してください。iniならGitHubセクションUserキーにユーザ名を指定してください。'
                )
            if not ('User' in config['GitHub']):
                raise Exception(
                    'ユーザ名が必要です。しかし起動引数にもconfig.iniにも存在しません。起動引数なら第一引数にユーザ名を渡してください。iniならGitHubセクションUserキーにユーザ名を指定してください。'
                )
            username = config['GitHub']['User']


#        print("username = {0}".format(username))
        web.log.Log.Log().Logger.info("username = {0}".format(username))
        account = self.__db.Accounts['Accounts'].find_one(Username=username)
        if None is account:
            raise Exception(
                'ユーザ {0} はDBのAccountsテーブルに存在しません。登録してから再度実行してください。'.format(
                    username))
        twofactor = self.__db.Accounts['TwoFactors'].find_one(
            AccountId=account['Id'])
        if None is twofactor:
            raise Exception(
                'ユーザ {0} はDBのTwoFactorsテーブルに存在しません。登録してから再度実行してください。'.format(
                    username))
        return twofactor['Secret']
 def __init__(self):
     self.__db = Database()
     self.__db.Initialize()
示例#8
0
 def __init__(self):
     self.__path_dir_this = os.path.abspath(os.path.dirname(__file__))
     self.__db = Database(self.__path_dir_this)
     #        self.__db = Database()
     self.__db.Initialize()