Skip to content

Commit a19fdd8

Browse files
authored
FEATURE: Change error raised by the SSO parse method to a custom error class (#248)
This commit changes the errors raise by the `parse` method of ``DiscourseApi::SingleSignOn` to raise a custom error classes instead of `RuntimeError` to make it possible to rescue errors specific to the `SingleSignOn` class. The new error classes inherit from `RuntimeError` to preserve backward compatibility, i.e. existing `rescue` blocks that target `RuntimeError` will continue to work. This commit also releases version 1.1.0.
1 parent dc43f64 commit a19fdd8

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.1.0] - 2022-07-05
10+
### Changed
11+
- `DiscourseApi::SingleSignOn.parse` now raises `DiscourseApi::SingleSignOn::ParseError` (inherits from `RuntimeError` to preserve backward compatibility) instead of `RuntimeError` when there's a signature mismatch.
12+
- `DiscourseApi::SingleSignOn.parse` now raises `DiscourseApi::SingleSignOn::MissingConfigError` (also inherits from `RuntimeError`) if `sso_secret` or `sso_url` are missing.
13+
914
## [1.0.0] - 2022-05-01
1015
### Changed
1116
- The package now requires ruby 2.6+

lib/discourse_api/single_sign_on.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
module DiscourseApi
77
class SingleSignOn
8+
class ParseError < RuntimeError; end
9+
class MissingConfigError < RuntimeError; end
10+
811
ACCESSORS = [
912
:add_groups,
1013
:admin,
@@ -52,11 +55,11 @@ class SingleSignOn
5255
attr_writer :custom_fields, :sso_secret, :sso_url
5356

5457
def self.sso_secret
55-
raise RuntimeError, "sso_secret not implemented on class, be sure to set it on instance"
58+
raise MissingConfigError, "sso_secret not implemented on class, be sure to set it on instance"
5659
end
5760

5861
def self.sso_url
59-
raise RuntimeError, "sso_url not implemented on class, be sure to set it on instance"
62+
raise MissingConfigError, "sso_url not implemented on class, be sure to set it on instance"
6063
end
6164

6265
def self.parse_hash(payload)
@@ -98,9 +101,9 @@ def self.parse(payload, sso_secret = nil)
98101
if sso.sign(parsed["sso"]) != parsed["sig"]
99102
diags = "\n\nsso: #{parsed["sso"]}\n\nsig: #{parsed["sig"]}\n\nexpected sig: #{sso.sign(parsed["sso"])}"
100103
if parsed["sso"] =~ /[^a-zA-Z0-9=\r\n\/+]/m
101-
raise RuntimeError, "The SSO field should be Base64 encoded, using only A-Z, a-z, 0-9, +, /, and = characters. Your input contains characters we don't understand as Base64, see http://en.wikipedia.org/wiki/Base64 #{diags}"
104+
raise ParseError, "The SSO field should be Base64 encoded, using only A-Z, a-z, 0-9, +, /, and = characters. Your input contains characters we don't understand as Base64, see http://en.wikipedia.org/wiki/Base64 #{diags}"
102105
else
103-
raise RuntimeError, "Bad signature for payload #{diags}"
106+
raise ParseError, "Bad signature for payload #{diags}"
104107
end
105108
end
106109

lib/discourse_api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# frozen_string_literal: true
22
module DiscourseApi
3-
VERSION = "1.0.0"
3+
VERSION = "1.1.0"
44
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe DiscourseApi::SingleSignOn do
6+
context "::MissingConfigError" do
7+
it "inherits from RuntimeError for backward compatibility" do
8+
expect(DiscourseApi::SingleSignOn::MissingConfigError).to be < RuntimeError
9+
end
10+
end
11+
12+
context "::ParseError" do
13+
it "inherits from RuntimeError for backward compatibility" do
14+
expect(DiscourseApi::SingleSignOn::ParseError).to be < RuntimeError
15+
end
16+
end
17+
18+
context ".sso_secret" do
19+
it "raises MissingConfigError when sso_secret is not present" do
20+
expect {
21+
described_class.sso_secret
22+
}.to raise_error(DiscourseApi::SingleSignOn::MissingConfigError)
23+
end
24+
end
25+
26+
context ".sso_url" do
27+
it "raises MissingConfigError when sso_url is not present" do
28+
expect {
29+
described_class.sso_url
30+
}.to raise_error(DiscourseApi::SingleSignOn::MissingConfigError)
31+
end
32+
end
33+
34+
context ".parse" do
35+
it "raises ParseError when there's a signature mismatch" do
36+
sso = described_class.new
37+
sso.sso_secret = "abcd"
38+
expect {
39+
described_class.parse(sso.payload, "dcba")
40+
}.to raise_error(DiscourseApi::SingleSignOn::ParseError)
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)