Skip to content

Commit

Permalink
Merge pull request wso2#1678 from pulasthi7/claim-based
Browse files Browse the repository at this point in the history
Add claim based authn template
  • Loading branch information
madurangasiriwardena authored Jul 3, 2018
2 parents 09a8ed8 + 3045aaa commit cbbc392
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"Step 2": "TOTP authenticator",
"Step 3": "FIDO authenticator"
},
"authenticationSteps":2,
"authenticationSteps":3,
"defaultAuthenticators": {
"1": {
"local": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,79 @@
{
"category": "User Based",
"name": "Claim Manipulation",
"name": "User Attribute Based",
"title": "User Attribute Based Authentication Template",
"summary": "Allow login to application if the user's age is over configured value. User's age is calculated using the user's date of birth attribute.",
"preRequisites": [
"Change the parameters at the top of the script as needed to match the requirements.",
"Modify the authentication option(s) from defaults as required."
],
"parametersDescription": {
"ageLimit" : "Minimum age required for the user to login to the application",
"errorPage" : "Error page to redirect user, if the age limit is below ageLimit",
"errorPageParameters" : "Parameters to be passed to the error page"
},
"defaultStepsDescription": {
"Step 1": "Basic (Password) authenticator"
},
"authenticationSteps": 1,
"defaultAuthenticators": {
"1": {
"local": [
"BasicAuthenticator"
],
"federated": []
}
},
"img": "./images/user.png",
"helpLink": "https://docs.wso2.com/display/IS570/Conditional+Authentication",
"code": [
"/*",
"This template shows how to manipulate user claims/attributes within the authentication flow",
"*/",
"function onInitialRequest (context) {",
" executeStep(1, {",
" onSuccess : function(context) {",
" var subject = context.steps[1].subject;",
" // Extracting existing claims/attributes from the authenticated subject in the step 1",
" var firstName = subject.localClaims['http://wso2.org/claims/givenname'];",
" var lastName = subject.localClaims['http://wso2.org/claims/lastname'];",
"// This script will only allow login to application if the user's age is over configured value.",
"// The user will be redirected to an error page if the date of birth is not present or user is below configured value.",
"",
"var ageLimit = 18;",
"",
"// Error page to redirect unauthorized users,",
"// can be either an absolute url or relative url to server root, or empty/null",
"// null/empty value will redirect to the default error page.",
"var errorPage = '';",
"",
"// Additional query params to be added to the above url.",
"// Hint: Use i18n keys for error messages.",
"var errorPageParameters = {",
" 'status': 'Unauthorized',",
" 'statusMsg': 'You need to be over ' + ageLimit + ' years to login to this application.'",
"};",
"",
" var displayName = firstName + ' ' + lastName;",
" // Setting new claims to the subject",
" subject.localClaims['http://wso2.org/claims/displayName'] = displayName;",
"// Date of birth attribute at the client side",
"var dateOfBirthClaim = 'http://wso2.org/claims/dob';",
"",
"function onInitialRequest(context) {",
" executeStep(1, {",
" onSuccess: function (context) {",
" var underAge = true;",
" // Extracting user store domain of authenticated subject from the first step",
" var dob = context.steps[1].subject.localClaims[dateOfBirthClaim];",
" if (dob && dob.match(/^(\\d{4})-(\\d{2})-(\\d{2})$/)) {",
" var birthDate = new Date(dob);",
" if (getAge(birthDate) >= ageLimit) {",
" underAge = false;",
" }",
" }",
" if (underAge === true) {",
" sendError(errorPage, errorPageParameters);",
" }",
" }",
" });",
"}",
"",
"function getAge(birthDate) {",
" var today = new Date();",
" var age = today.getFullYear() - birthDate.getFullYear();",
" var m = today.getMonth() - birthDate.getMonth();",
" if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {",
" age--;",
" }",
" return age;",
"}"
],
"help": "Manipulating claims/attributes of the authenticated subject.",
"helpLink": "https://docs.wso2.com/display/IS560/Conditional+Authentication"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@
"",
"function onInitialRequest(context) {",
" executeStep(1, {",
" onSuccess: {",
" function (context) {",
" // Extracting tenant domain of authenticated subject from the first step",
" var userTenantDomain = context.steps[1].subject.tenantDomain;",
" // Checking if the user is from whitelisted tenant domain",
" if (tenantsToStepUp.indexOf(userTenantDomain) >= 0) {",
" executeStep(2);",
" }",
" onSuccess: function (context) {",
" // Extracting tenant domain of authenticated subject from the first step",
" var userTenantDomain = context.steps[1].subject.tenantDomain;",
" // Checking if the user is from whitelisted tenant domain",
" if (tenantsToStepUp.indexOf(userTenantDomain) >= 0) {",
" executeStep(2);",
" }",
" }",
" });",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,17 @@
"img": "./images/user.png",
"code": [
"// This script will prompt 2FA to the app only for a selected set of user stores.",
"",
"// If the user is in one of the following user stores, user will be prompted 2FA",
"var userStoresToStepUp = ['EMPLOYEES', 'CONTRACTORS'];",
"",
"function onInitialRequest(context) {",
" executeStep(1, {",
" onSuccess: {",
" function (context) {",
" // Extracting user store domain of authenticated subject from the first step",
" var userStoreDomain = context.steps[1].subject.userStoreDomain;",
" // Checking if the user is from whitelisted tenant domain",
" if (userStoresToStepUp.indexOf(userStoreDomain) >= 0) {",
" executeStep(2);",
" }",
" onSuccess: function (context) {",
" // Extracting user store domain of authenticated subject from the first step",
" var userStoreDomain = context.steps[1].subject.userStoreDomain;",
" // Checking if the user is from whitelisted tenant domain",
" if (userStoresToStepUp.indexOf(userStoreDomain) >= 0) {",
" executeStep(2);",
" }",
" }",
" });",
Expand Down

0 comments on commit cbbc392

Please sign in to comment.