Skip to content

Commit 85f0b06

Browse files
Andrew-Lees11djones6
authored andcommitted
Feat: Add example Claims implementations (#11)
1 parent 71a6250 commit 85f0b06

File tree

3 files changed

+372
-0
lines changed

3 files changed

+372
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright IBM Corporation 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
import Foundation
18+
19+
// MARK MicroProfileClaims
20+
21+
/// A class representing the MicroProfile claims as listed in [MicroProfile specs](http://microprofile.io/project/eclipse/microprofile-jwt-auth/spec/src/main/asciidoc/interoperability.asciidoc).
22+
public class ClaimsMicroProfile: Claims {
23+
24+
public init(
25+
iss: String,
26+
sub: String,
27+
exp: Date,
28+
iat: Date,
29+
jti: String,
30+
upn: String,
31+
groups: [String]
32+
) {
33+
self.iss = iss
34+
self.sub = sub
35+
self.exp = exp
36+
self.iat = iat
37+
self.jti = jti
38+
self.upn = upn
39+
self.groups = groups
40+
}
41+
/**
42+
The MP-JWT issuer. [RFC7519, Section 4.1.1](https://tools.ietf.org/html/rfc7519#section-4.1.1)
43+
*/
44+
public var iss: String
45+
46+
/**
47+
Identifies the principal that is the subject of the JWT.
48+
*/
49+
public var sub: String
50+
51+
/**
52+
Identifies the expiration time on or after which the JWT MUST NOT be accepted for processing.
53+
*/
54+
public var exp: Date
55+
56+
/**
57+
Identifies the time at which the JWT was issued.
58+
*/
59+
public var iat: Date
60+
61+
/**
62+
The "jti" (JWT ID) claim provides a unique identifier for the JWT.
63+
The identifier value MUST be assigned in a manner that ensures that
64+
there is a negligible probability that the same value will be
65+
accidentally assigned to a different data object.
66+
*/
67+
public var jti: String
68+
69+
/**
70+
This MP-JWT custom claim is the user principal name in the java.security.Principal interface, and is the caller principal name in javax.security.enterprise.identitystore.IdentityStore. If this claim is missing, fallback to the "preferred_username", should be attempted, and if that claim is missing, fallback to the "sub" claim should be used.
71+
*/
72+
public var upn: String?
73+
74+
/**
75+
Shorthand name by which the End-User wishes to be referred to at the RP, such as janedoe or j.doe. This value MAY be any valid JSON string including special characters such as @, /, or whitespace.
76+
*/
77+
public var preferred_username: String?
78+
79+
/**
80+
This MP-JWT custom claim is the list of group names that have been assigned to the principal of the MP-JWT. This typically will required a mapping at the application container level to application deployment roles, but a one-to-one between group names and application role names is required to be performed in addition to any other mapping.
81+
*/
82+
public var groups: [String]
83+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/**
2+
* Copyright IBM Corporation 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
import Foundation
18+
19+
// MARK OpenIDClaims
20+
21+
/// A class representing OpenID related claims as decsribed in [OpenID specs](http://openid.net/specs/openid-connect-core-1_0.html).
22+
public class ClaimsOpenID: Claims {
23+
24+
/// Initalise the OpenIDClaims
25+
public init(
26+
iss: String,
27+
sub: String,
28+
aud: [String],
29+
exp: Date,
30+
iat: Date,
31+
auth_time: Date? = nil,
32+
nonce: String? = nil,
33+
acr: String? = nil,
34+
amr: [String]? = nil,
35+
azp: String? = nil,
36+
name: String? = nil,
37+
given_name: String? = nil,
38+
family_name: String? = nil,
39+
middle_name: String? = nil,
40+
nickname: String? = nil,
41+
preferred_username: String? = nil,
42+
profile: String? = nil,
43+
picture: String? = nil,
44+
website: String? = nil,
45+
email: String? = nil,
46+
email_verified: Bool? = nil,
47+
gender: String? = nil,
48+
birthdate: String? = nil,
49+
zoneinfo: String? = nil,
50+
locale: String? = nil,
51+
phone_number: String? = nil,
52+
phone_number_verified: Bool? = nil,
53+
address: AddressClaim? = nil,
54+
updated_at: Date? = nil
55+
) {
56+
self.iss = iss
57+
self.sub = sub
58+
self.aud = aud
59+
self.exp = exp
60+
self.iat = iat
61+
self.auth_time = auth_time
62+
self.nonce = nonce
63+
self.acr = acr
64+
self.amr = amr
65+
self.azp = azp
66+
self.name = name
67+
self.given_name = given_name
68+
self.family_name = family_name
69+
self.middle_name = middle_name
70+
self.nickname = nickname
71+
self.preferred_username = preferred_username
72+
self.profile = profile
73+
self.picture = picture
74+
self.website = website
75+
self.email = email
76+
self.email_verified = email_verified
77+
self.gender = gender
78+
self.birthdate = birthdate
79+
self.zoneinfo = zoneinfo
80+
self.locale = locale
81+
self.phone_number = phone_number
82+
self.phone_number_verified = phone_number_verified
83+
self.address = address
84+
self.updated_at = updated_at
85+
}
86+
87+
// MARK: ID Token
88+
89+
/// Issuer Identifier for the Issuer of the response. The iss value is a case sensitive URL using the https scheme that contains scheme, host, and optionally, port number and path components and no query or fragment components.
90+
public var iss: String
91+
92+
/// Subject Identifier. A locally unique and never reassigned identifier within the Issuer for the End-User, which is intended to be consumed by the Client, e.g., 24400320 or AItOawmwtWwcT0k51BayewNvutrJUqsvl6qs7A4. It MUST NOT exceed 255 ASCII characters in length. The sub value is case sensitive.
93+
public var sub: String
94+
95+
/// Audience(s) that this ID Token is intended for. It MUST contain the OAuth 2.0 client_id of the Relying Party as an audience value. It MAY also contain identifiers for other audiences.
96+
public var aud: [String]
97+
98+
/// Expiration time on or after which the ID Token MUST NOT be accepted for processing. The processing of this parameter requires that the current date/time MUST be before the expiration date/time listed in the value. Implementers MAY provide for some small leeway, usually no more than a few minutes, to account for clock skew.
99+
public var exp: Date
100+
101+
/// Time at which the JWT was issued.
102+
public var iat: Date
103+
104+
/// Time when the End-User authentication occurred.
105+
public var auth_time: Date?
106+
107+
/// String value used to associate a Client session with an ID Token, and to mitigate replay attacks. The value is passed through unmodified from the Authentication Request to the ID Token. If present in the ID Token, Clients MUST verify that the nonce Claim Value is equal to the value of the nonce parameter sent in the Authentication Request. If present in the Authentication Request, Authorization Servers MUST include a nonce Claim in the ID Token with the Claim Value being the nonce value sent in the Authentication Request. Authorization Servers SHOULD perform no other processing on nonce values used.
108+
public var nonce: String?
109+
110+
/// Authentication Context Class Reference. String specifying an Authentication Context Class Reference value that identifies the Authentication Context Class that the authentication performed satisfied. The value "0" indicates the End-User authentication did not meet the requirements of ISO/IEC 29115 level 1. Authentications with level 0 SHOULD NOT be used to authorize access to any resource of any monetary value. Parties using this claim will need to agree upon the meanings of the values used, which may be context-specific.
111+
public var acr: String?
112+
113+
/// Authentication Methods References. JSON array of strings that are identifiers for authentication methods used in the authentication. For instance, values might indicate that both password and OTP authentication methods were used. Parties using this claim will need to agree upon the meanings of the values used, which may be context-specific.
114+
public var amr: [String]?
115+
116+
/// Authorized party - the party to which the ID Token was issued. If present, it MUST contain the OAuth 2.0 Client ID of this party. This Claim is only needed when the ID Token has a single audience value and that audience is different than the authorized party. It MAY be included even when the authorized party is the same as the sole audience.
117+
public var azp: String?
118+
119+
// MARK: Standard Claims
120+
121+
/// End-User's full name in displayable form including all name parts, possibly including titles and suffixes, ordered according to the End-User's locale and preferences.
122+
public var name: String?
123+
124+
/// Given name(s) or first name(s) of the End-User. Note that in some cultures, people can have multiple given names; all can be present, with the names being separated by space characters.
125+
public var given_name: String?
126+
127+
/// Surname(s) or last name(s) of the End-User. Note that in some cultures, people can have multiple family names or no family name; all can be present, with the names being separated by space characters.
128+
public var family_name: String?
129+
130+
/// Middle name(s) of the End-User. Note that in some cultures, people can have multiple middle names; all can be present, with the names being separated by space characters. Also note that in some cultures, middle names are not used.
131+
public var middle_name: String?
132+
133+
/// Casual name of the End-User that may or may not be the same as the given_name. For instance, a nickname value of Mike might be returned alongside a given_name value of Michael.
134+
public var nickname: String?
135+
136+
/// Shorthand name by which the End-User wishes to be referred to at the RP, such as janedoe or j.doe. This value MAY be any valid JSON string including special characters such as @, /, or whitespace.
137+
public var preferred_username: String?
138+
139+
/// URL of the End-User's profile page. The contents of this Web page SHOULD be about the End-User.
140+
public var profile: String?
141+
142+
/// URL of the End-User's profile picture. This URL MUST refer to an image file (for example, a PNG, JPEG, or GIF image file), rather than to a Web page containing an image. Note that this URL SHOULD specifically reference a profile photo of the End-User suitable for displaying when describing the End-User, rather than an arbitrary photo taken by the End-User.
143+
public var picture: String?
144+
145+
/// URL of the End-User's Web page or blog. This Web page SHOULD contain information published by the End-User or an organization that the End-User is affiliated with.
146+
public var website: String?
147+
148+
/// End-User's preferred e-mail address.
149+
public var email: String?
150+
151+
/// True if the End-User's e-mail address has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this e-mail address was controlled by the End-User at the time the verification was performed. The means by which an e-mail address is verified is context-specific, and dependent upon the trust framework or contractual agreements within which the parties are operating.
152+
public var email_verified: Bool?
153+
154+
/// End-User's gender. Values defined by this specification are female and male. Other values MAY be used when neither of the defined values are applicable.
155+
public var gender: String?
156+
157+
/// End-User's birthday, represented as an ISO 8601:2004 YYYY-MM-DD format. The year MAY be 0000, indicating that it is omitted. To represent only the year, YYYY format is allowed.
158+
public var birthdate: String?
159+
160+
/// String from zoneinfo time zone database representing the End-User's time zone. For example, Europe/Paris or America/Los_Angeles.
161+
public var zoneinfo: String?
162+
163+
/// End-User's locale, represented as a BCP47 language tag. This is typically an ISO 639-1 Alpha-2 language code in lowercase and an ISO 3166-1 Alpha-2 country code in uppercase, separated by a dash. For example, en-US or fr-CA. As a compatibility note, some implementations have used an underscore as the separator rather than a dash, for example, en_US; Relying Parties MAY choose to accept this locale syntax as well.
164+
public var locale: String?
165+
166+
/// End-User's preferred telephone number. E.164 is RECOMMENDED as the format of this Claim, for example, +1 (425) 555-1212 or +56 (2) 687 2400.
167+
public var phone_number: String?
168+
169+
/// True if the End-User's phone number has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this phone number was controlled by the End-User at the time the verification was performed. The means by which a phone number is verified is context-specific, and dependent upon the trust framework or contractual agreements within which the parties are operating. When true, the phone_number Claim MUST be in E.164 format and any extensions MUST be represented in RFC 3966 format.
170+
public var phone_number_verified: Bool?
171+
172+
/// End-User's preferred postal address.
173+
public var address: AddressClaim?
174+
175+
/// Time the End-User's information was last updated.
176+
public var updated_at: Date?
177+
}
178+
179+
/// Struct representing an AddressClaim as defined in the [OpenID specs](http://openid.net/specs/openid-connect-core-1_0.html).
180+
public struct AddressClaim: Codable {
181+
182+
/// Full mailing address, formatted for display or use on a mailing label. This field MAY contain multiple lines, separated by newlines. Newlines can be represented either as a carriage return/line feed pair ("\r\n") or as a single line feed character ("\n").
183+
public var formatted: String?
184+
185+
/// Full street address component, which MAY include house number, street name, Post Office Box, and multi-line extended street address information. This field MAY contain multiple lines, separated by newlines. Newlines can be represented either as a carriage return/line feed pair ("\r\n") or as a single line feed character ("\n").
186+
public var street_address: String?
187+
188+
/// City or locality component.
189+
public var locality: String?
190+
191+
/// State, province, prefecture, or region component.
192+
public var region: String?
193+
194+
/// Zip code or postal code component.
195+
public var postal_code: String?
196+
197+
/// Country name component.
198+
public var country: String?
199+
200+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Copyright IBM Corporation 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
import Foundation
18+
19+
// MARK StandardJWTClaims
20+
21+
/// A class representing the Standard JWT claims as described in [RFC7519](https://tools.ietf.org/html/rfc7519#section-4.1).
22+
public class ClaimsStandardJWT: Claims {
23+
24+
/**
25+
The "iss" (issuer) claim identifies the principal that issued the
26+
JWT. The processing of this claim is generally application specific.
27+
The "iss" value is a case-sensitive.
28+
*/
29+
public var iss: String?
30+
31+
/**
32+
The "sub" (subject) claim identifies the principal that is the
33+
subject of the JWT. The claims in a JWT are normally statements
34+
about the subject. The subject value MUST either be scoped to be
35+
locally unique in the context of the issuer or be globally unique.
36+
The processing of this claim is generally application specific. The
37+
"sub" value is case-sensitive.
38+
*/
39+
public var sub: String?
40+
41+
/**
42+
The "aud" (audience) claim identifies the recipients that the JWT is
43+
intended for. Each principal intended to process the JWT MUST
44+
identify itself with a value in the audience claim. If the principal
45+
processing the claim does not identify itself with a value in the
46+
"aud" claim when this claim is present, then the JWT MUST be
47+
rejected. The interpretation of audience values is generally application specific.
48+
The "aud" value is case-sensitive.
49+
*/
50+
public var aud: [String]?
51+
52+
/**
53+
The "exp" (expiration time) claim identifies the expiration time on
54+
or after which the JWT MUST NOT be accepted for processing. The
55+
processing of the "exp" claim requires that the current date/time
56+
MUST be before the expiration date/time listed in the "exp" claim.
57+
Implementers MAY provide for some small leeway, usually no more than
58+
a few minutes, to account for clock skew.
59+
*/
60+
public var exp: Date?
61+
62+
/**
63+
The "nbf" (not before) claim identifies the time before which the JWT
64+
MUST NOT be accepted for processing. The processing of the "nbf"
65+
claim requires that the current date/time MUST be after or equal to
66+
the not-before date/time listed in the "nbf" claim. Implementers MAY
67+
provide for some small leeway, usually no more than a few minutes, to
68+
account for clock skew.
69+
*/
70+
public var nbf: Date?
71+
72+
/**
73+
The "iat" (issued at) claim identifies the time at which the JWT was
74+
issued. This claim can be used to determine the age of the JWT.
75+
*/
76+
public var iat: Date?
77+
78+
/**
79+
The "jti" (JWT ID) claim provides a unique identifier for the JWT.
80+
The identifier value MUST be assigned in a manner that ensures that
81+
there is a negligible probability that the same value will be
82+
accidentally assigned to a different data object; if the application
83+
uses multiple issuers, collisions MUST be prevented among values
84+
produced by different issuers as well. The "jti" claim can be used
85+
to prevent the JWT from being replayed. The "jti" value is case-
86+
sensitive
87+
*/
88+
public var jti: String?
89+
}

0 commit comments

Comments
 (0)