def setUp(self):
        self.client = FactorsClient(base_url="https://mockta.com",
                                    api_token="abcdefg")
        self.user_id = "00upofwtwaGmrmIsm0h7"
        self.factor_id = "ufss082hlipGNzCff0h7"

        with open("tests/data/factor.json", "r") as file:
            self.factor = file.read()

        with open("tests/data/factors.json", "r") as file:
            self.factors = file.read()
class FactorsClientTest(unittest.TestCase):

    def setUp(self):
        self.client = FactorsClient(base_url="https://mockta.com",
                                    api_token="abcdefg")
        self.user_id = "00upofwtwaGmrmIsm0h7"
        self.factor_id = "ufss082hlipGNzCff0h7"

        with open("tests/data/factor.json", "r") as file:
            self.factor = file.read()

        with open("tests/data/factors.json", "r") as file:
            self.factors = file.read()

    def tearDown(self):
        pass

    @patch("okta.framework.ApiClient.requests.get")
    def test_get_factors(self, mock_get):
        # Configure the mock to return a response with an OK status code
        # and the raw text response (unparsed JSON)
        mock_get.return_value = Mock(status_code=200, text=self.factors)
        response = self.client.get_lifecycle_factors(self.user_id)
        factor_json = Utils.deserialize(self.factors, Factor)

        self.assertIsNotNone(response)
        self.assertIsInstance(response, list)
        self.assertEqual(len(response), len(factor_json))
        self.assertIsInstance(response[0], Factor)

    @patch("okta.framework.ApiClient.requests.get")
    def test_get_factor_by_id(self, mock_get):
        mock_get.return_value = Mock(status_code=200, text=self.factor)
        response = self.client.get_factor(self.user_id, self.factor_id)

        self.assertIsNotNone(response)
        self.assertIsInstance(response, Factor)
        self.assertEqual(response.id, self.factor_id)
from okta.UsersClient import UsersClient
from okta.FactorsClient import FactorsClient
from okta.framework.OktaError import OktaError
from script_config import base_url, api_token, user_id

usersClient = UsersClient(base_url=base_url, api_token=api_token)
factorsClient = FactorsClient(base_url=base_url, api_token=api_token)

user = usersClient.get_user(user_id)

print("Enroll SMS factor started")
phone_number = input("Please enter your phone number (just numbers please): ")
enroll_request = {
    "factorType": "sms",
    "provider": "OKTA",
    "profile": {
        "phoneNumber": phone_number
    }
}

try:
    response = factorsClient.enroll_factor(user_id, enroll_request)
    result = response.status
    factor_id = response.id
except OktaError as e:
    print(e.error_causes)
    exit(2)

while result == "PENDING_ACTIVATION":
    # active when done
    try:
from okta.UsersClient import UsersClient
from okta.FactorsClient import FactorsClient
from okta.framework.OktaError import OktaError
from script_config import base_url, api_token, user_id

usersClient = UsersClient(base_url=base_url, api_token=api_token)
factorsClient = FactorsClient(base_url=base_url, api_token=api_token)

user = usersClient.get_user(user_id)
available_questions = factorsClient.get_available_questions(user_id)

num = 1
for question in available_questions:
    print("{0}.) {1}".format(num, question.questionText))
    num = num + 1

chosen_number = int(input("\nPlease choose a question: "))
chosen_question = available_questions[chosen_number - 1]
question = chosen_question.question
answer = input("{0}: ".format(chosen_question.questionText))

enroll_request = {
    "factorType": "question",
    "provider": "OKTA",
    "profile": {
        "question": question,
        "answer": answer
    }
}

print("Enroll question factor started")
import json
import time

from okta.FactorsClient import FactorsClient
from okta.models.factor.Factor import Factor
from okta.framework.OktaError import OktaError
from okta.framework.Serializer import Serializer
from script_config import base_url, api_token, user_id

factorsClient = FactorsClient(base_url=base_url, api_token=api_token)

enroll_request = {"factorType": "push", "provider": "OKTA"}

print("Enroll push factor started")
try:
    response = factorsClient.enroll_factor(user_id, enroll_request)
    poll_url = response.links.get("poll").href
    qrcode_url = response.embedded.get("activation").links.get("qrcode").href
    expires_at = response.embedded.get("activation").expiresAt
    print("Expires at: {0}\n".format(expires_at))
    print("{0}\n".format(qrcode_url))
except OktaError as e:
    print(e.error_causes)
    exit(2)

factor_result = response.embedded.get("activation").factorResult
while factor_result == "WAITING":
    poll_response = factorsClient.push_activation_poll(poll_url)
    factor_result = poll_response.factorResult or poll_response.status
    if factor_result == "WAITING":
        print("Waiting for confirmation of push enrollment")
from okta.UsersClient import UsersClient
from okta.FactorsClient import FactorsClient
from okta.framework.OktaError import OktaError
from script_config import api_token, base_url, user_id, get_factor_id_by_type

usersClient = UsersClient(base_url=base_url, api_token=api_token)
factorsClient = FactorsClient(base_url=base_url, api_token=api_token)

user = usersClient.get_user(user_id)
factors = factorsClient.get_lifecycle_factors(user.id)
factor_id, factor_profile = get_factor_id_by_type(factors,
                                                  "token:software:totp",
                                                  "GOOGLE")

if factor_id == None:
    print("No Google Authenticator factor enrolled")
    exit(2)

print("Verifying Google Authenticator")
result = "WAITING"

while result != "SUCCESS":
    try:
        pass_code = input("Enter your OTP: ")
        response = factorsClient.verify_factor(user_id,
                                               factor_id,
                                               passcode=pass_code)
        result = response.factorResult
        print("Google Authenticator verification passed")
    except OktaError as e:
        print(e.error_causes)
示例#7
0
from flask import Blueprint, jsonify, make_response, request
from flask import current_app as app
from flask_api import status

from okta.FactorsClient import FactorsClient
from okta.framework.OktaError import OktaError
from okta.models.factor.Factor import Factor
from okta.models.factor.FactorEnrollRequest import FactorEnrollRequest

from oktaadminapi.decorators import authenticated

bp = Blueprint("factors", __name__)
client = FactorsClient(base_url=app.config.get("ORG_NAME"),
                       api_token=app.config.get("API_TOKEN"))


@bp.route("/<user_id>/factors/<factor_id>", methods=["GET"])
@authenticated
def get_factor(user_id, factor_id):
    """
    Gets a factor
    """
    app.logger.debug("get_factor({0}, {1})".format(user_id, factor_id))
    try:
        response = client.get_factor(user_id, factor_id)
        return jsonify(response)
    except OktaError as e:
        message = {
            "error_causes": e.error_causes,
            "error_summary": e.error_summary,
            "error_id": e.error_id,
import json
import time

from okta.UsersClient import UsersClient
from okta.FactorsClient import FactorsClient
from okta.framework.OktaError import OktaError
from okta.framework.Serializer import Serializer
from script_config import api_token, base_url, user_id, get_factor_id_by_type

usersClient = UsersClient(base_url=base_url, api_token=api_token)
factorsClient = FactorsClient(base_url=base_url, api_token=api_token)

user = usersClient.get_user(user_id)
factors = factorsClient.get_lifecycle_factors(user.id)
factor_id, factor_profile = get_factor_id_by_type(factors, "push")

if factor_id == None:
    print("No Okta Verify Push factor enrolled")
    exit(2)

print("Issuing Okta Verify Push challenge: {0}".format(factor_profile.name))

# issue the challenge
response = factorsClient.verify_factor(user.id, factor_id)
result = response.factorResult
polling_url = response.links.get("poll").href

while result == "WAITING":
    try:
        response = factorsClient.push_verification_poll(polling_url)
        result = response.factorResult
import getpass
import json

from okta.UsersClient import UsersClient
from okta.FactorsClient import FactorsClient
from okta.framework.OktaError import OktaError
from script_config import api_token, base_url, user_id, get_factor_id_by_type

usersClient = UsersClient(base_url=base_url, api_token=api_token)
factorsClient = FactorsClient(base_url=base_url, api_token=api_token)

user = usersClient.get_user(user_id)
factors = factorsClient.get_lifecycle_factors(user.id)
factor_id, factor_profile = get_factor_id_by_type(factors, "question")

if factor_id == None:
    print("No Security Question factor enrolled")
    exit(2)

print("Verifying security question")
question = factor_profile.questionText
result = "WAITING"

while result != "SUCCESS":
    try:
        answer = getpass.getpass("{0}: ".format(question))
        response = factorsClient.verify_factor(user.id,
                                               factor_id,
                                               answer=answer)
        result = response.factorResult
        print("Security question verification passed")