diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3eeffc5..b577c3f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,9 +14,12 @@ def info end def authenticate_request - token = request.headers["Authorization"] - token = token.split(" ").last if token - @identity = Services::AuthService.new.authenticate!(token) + result = AuthenticateIdentity.call(request: request) + if result.success? + @identity = result.identity + else + raise AuthError + end end private diff --git a/app/controllers/certificates_controller.rb b/app/controllers/certificates_controller.rb index 4528663..b59fc74 100644 --- a/app/controllers/certificates_controller.rb +++ b/app/controllers/certificates_controller.rb @@ -5,9 +5,12 @@ def create req = CertIssueRequest.new(params_permitted) if !req.valid? render json: { error: req.errors }, status: :bad_request + end + result = IssueCert.call(request: req) + if result.success? + render json: result.cert else - cert = Services::CertificateService.new.issue_cert(req) - render json: cert + raise StandardError.new result.message end end diff --git a/app/interactors/authenticate_identity.rb b/app/interactors/authenticate_identity.rb new file mode 100644 index 0000000..aa4de8c --- /dev/null +++ b/app/interactors/authenticate_identity.rb @@ -0,0 +1,16 @@ +class AuthenticateIdentity + include Interactor + + before do + token = context.request.headers["Authorization"] + context.token = token.split(" ").last if token + end + + def call + if identity = Services::AuthService.new.authenticate!(context.token) + context.identity = identity + else + context.fail! + end + end +end diff --git a/app/interactors/check_policy.rb b/app/interactors/check_policy.rb new file mode 100644 index 0000000..5aaa656 --- /dev/null +++ b/app/interactors/check_policy.rb @@ -0,0 +1,6 @@ +class CheckPolicy + include Interactor + + def call + end +end diff --git a/app/interactors/issue_cert.rb b/app/interactors/issue_cert.rb new file mode 100644 index 0000000..e3456af --- /dev/null +++ b/app/interactors/issue_cert.rb @@ -0,0 +1,5 @@ +class IssueCert + include Interactor::Organizer + + organize CheckPolicy, ObtainCert +end diff --git a/app/interactors/obtain_cert.rb b/app/interactors/obtain_cert.rb new file mode 100644 index 0000000..65ffe59 --- /dev/null +++ b/app/interactors/obtain_cert.rb @@ -0,0 +1,7 @@ +class ObtainCert + include Interactor + + def call + context.cert = Services::CertificateService.new.issue_cert(context.request) + end +end