...

Source file src/github.com/ory/fosite/scope_strategy.go

Documentation: github.com/ory/fosite

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     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   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   *
    20   */
    21  
    22  package fosite
    23  
    24  import "strings"
    25  
    26  // ScopeStrategy is a strategy for matching scopes.
    27  type ScopeStrategy func(haystack []string, needle string) bool
    28  
    29  func HierarchicScopeStrategy(haystack []string, needle string) bool {
    30  	for _, this := range haystack {
    31  		// foo == foo -> true
    32  		if this == needle {
    33  			return true
    34  		}
    35  
    36  		// picture.read > picture -> false (scope picture includes read, write, ...)
    37  		if len(this) > len(needle) {
    38  			continue
    39  		}
    40  
    41  		needles := strings.Split(needle, ".")
    42  		haystack := strings.Split(this, ".")
    43  		haystackLen := len(haystack) - 1
    44  		for k, needle := range needles {
    45  			if haystackLen < k {
    46  				return true
    47  			}
    48  
    49  			current := haystack[k]
    50  			if current != needle {
    51  				break
    52  			}
    53  		}
    54  	}
    55  
    56  	return false
    57  }
    58  
    59  func ExactScopeStrategy(haystack []string, needle string) bool {
    60  	for _, this := range haystack {
    61  		if needle == this {
    62  			return true
    63  		}
    64  	}
    65  
    66  	return false
    67  }
    68  
    69  func WildcardScopeStrategy(matchers []string, needle string) bool {
    70  	needleParts := strings.Split(needle, ".")
    71  	for _, matcher := range matchers {
    72  		matcherParts := strings.Split(matcher, ".")
    73  
    74  		if len(matcherParts) > len(needleParts) {
    75  			continue
    76  		}
    77  
    78  		var noteq bool
    79  		for k, c := range strings.Split(matcher, ".") {
    80  			// this is the last item and the lengths are different
    81  			if k == len(matcherParts)-1 && len(matcherParts) != len(needleParts) {
    82  				if c != "*" {
    83  					noteq = true
    84  					break
    85  				}
    86  			}
    87  
    88  			if c == "*" && len(needleParts[k]) > 0 {
    89  				// pass because this satisfies the requirements
    90  				continue
    91  			} else if c != needleParts[k] {
    92  				noteq = true
    93  				break
    94  			}
    95  		}
    96  
    97  		if !noteq {
    98  			return true
    99  		}
   100  	}
   101  
   102  	return false
   103  }
   104  

View as plain text