2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.aaa.authz.srv;
11 import java.util.List;
12 import org.opendaylight.aaa.api.Authentication;
13 import org.opendaylight.aaa.api.AuthenticationService;
14 import org.opendaylight.controller.config.yang.config.aaa_authz.srv.Policies;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.yang.gen.v1.urn.aaa.yang.authz.ds.rev140722.ActionType;
17 import org.opendaylight.yang.gen.v1.urn.aaa.yang.authz.ds.rev140722.AuthorizationResponseType;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 * @author lmukkama Date: 9/2/14
23 public class AuthzServiceImpl {
25 private static List<Policies> listPolicies;
27 private static final String WILDCARD_TOKEN = "*";
29 public static boolean isAuthorized(LogicalDatastoreType logicalDatastoreType,
30 YangInstanceIdentifier yangInstanceIdentifier, ActionType actionType) {
32 AuthorizationResponseType authorizationResponseType = AuthzServiceImpl.reqAuthorization(
33 actionType, logicalDatastoreType, yangInstanceIdentifier);
34 return authorizationResponseType.equals(AuthorizationResponseType.Authorized);
37 public static boolean isAuthorized(ActionType actionType) {
38 AuthorizationResponseType authorizationResponseType = AuthzServiceImpl
39 .reqAuthorization(actionType);
40 return authorizationResponseType.equals(AuthorizationResponseType.Authorized);
43 public static void setPolicies(List<Policies> policies) {
45 AuthzServiceImpl.listPolicies = policies;
48 public static AuthorizationResponseType reqAuthorization(ActionType actionType) {
50 AuthenticationService authenticationService = AuthzDomDataBroker.getInstance()
52 if (authenticationService != null && AuthzServiceImpl.listPolicies != null
53 && AuthzServiceImpl.listPolicies.size() > 0) {
54 Authentication authentication = authenticationService.get();
55 if (authentication != null && authentication.roles() != null
56 && authentication.roles().size() > 0) {
57 return checkAuthorization(actionType, authentication);
60 return AuthorizationResponseType.NotAuthorized;
63 public static AuthorizationResponseType reqAuthorization(ActionType actionType,
64 LogicalDatastoreType logicalDatastoreType, YangInstanceIdentifier yangInstanceIdentifier) {
66 AuthenticationService authenticationService = AuthzDomDataBroker.getInstance()
69 if (authenticationService != null && AuthzServiceImpl.listPolicies != null
70 && AuthzServiceImpl.listPolicies.size() > 0) {
71 // Authentication Service exists. Can do authorization checks
72 Authentication authentication = authenticationService.get();
74 if (authentication != null && authentication.roles() != null
75 && authentication.roles().size() > 0) {
76 // Authentication claim object exists with atleast one role
77 return checkAuthorization(actionType, authentication, logicalDatastoreType,
78 yangInstanceIdentifier);
82 return AuthorizationResponseType.Authorized;
85 private static AuthorizationResponseType checkAuthorization(ActionType actionType,
86 Authentication authentication, LogicalDatastoreType logicalDatastoreType,
87 YangInstanceIdentifier yangInstanceIdentifier) {
89 for (Policies policy : AuthzServiceImpl.listPolicies) {
91 // Action type is compared as string, since its type is string in
92 // the config yang. Comparison is case insensitive
93 if (authentication.roles().contains(policy.getRole().getValue())
94 && (policy.getResource().getValue().equals(WILDCARD_TOKEN) || policy
95 .getResource().getValue().equals(yangInstanceIdentifier.toString()))
96 && (policy.getAction().toLowerCase()
97 .equals(ActionType.Any.name().toLowerCase()) || actionType.name()
98 .toLowerCase().equals(policy.getAction().toLowerCase()))) {
100 return AuthorizationResponseType.Authorized;
105 // For helium release we unauthorize other requests.
106 return AuthorizationResponseType.NotAuthorized;
109 private static AuthorizationResponseType checkAuthorization(ActionType actionType,
110 Authentication authentication) {
112 for (Policies policy : AuthzServiceImpl.listPolicies) {
113 if (authentication.roles().contains(policy.getRole().getValue())
114 && (policy.getAction().equalsIgnoreCase(ActionType.Any.name()) || policy
115 .getAction().equalsIgnoreCase(actionType.name()))) {
116 return AuthorizationResponseType.Authorized;
119 return AuthorizationResponseType.NotAuthorized;