File: access_scopes.go

package info (click to toggle)
golang-github-microsoft-dev-tunnels 0.0.25-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,988 kB
  • sloc: cs: 9,969; java: 2,767; javascript: 328; xml: 186; makefile: 5
file content (61 lines) | stat: -rw-r--r-- 1,301 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package tunnels

import (
	"fmt"
	"strings"
)

var (
	allScopes = map[TunnelAccessScope]bool{
		TunnelAccessScopeManage:      true,
		TunnelAccessScopeManagePorts: true,
		TunnelAccessScopeHost:        true,
		TunnelAccessScopeInspect:     true,
		TunnelAccessScopeConnect:     true,
	}
)

func (s *TunnelAccessScopes) valid(validScopes []TunnelAccessScope, allowMultiple bool) error {
	if s == nil {
		return fmt.Errorf("scopes cannot be null")
	}

	var scopes TunnelAccessScopes
	if allowMultiple {
		for _, scope := range *s {
			for _, ss := range strings.Split(string(scope), " ") {
				scopes = append(scopes, TunnelAccessScope(ss))
			}
		}
	} else {
		scopes = *s
	}

	for _, scope := range scopes {
		if len(scope) == 0 {
			return fmt.Errorf("scope cannot be null")
		} else if !allScopes[scope] {
			return fmt.Errorf("invalid scope %s", scope)
		}
	}
	if len(validScopes) > 0 {
		for _, scope := range scopes {
			if !scopeContains(validScopes, scope) {
				return fmt.Errorf("tunnel access scope is invalid for current request: %s", scope)
			}
		}
	}
	return nil
}

func scopeContains(s []TunnelAccessScope, e TunnelAccessScope) bool {
	for _, a := range s {
		if a == e {
			return true
		}
	}
	return false
}