Skip to content
Prev Previous commit
Next Next commit
fixup! Merge branch 'main' into dean/token-scopes
  • Loading branch information
deansheather committed Sep 16, 2022
commit 6bd2398be60a39d408a46a7d60d9ec6cfb010b96
22 changes: 3 additions & 19 deletions coderd/database/modelmethods.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
package database

import (
"golang.org/x/xerrors"

"github.com/coder/coder/coderd/rbac"
)

var validAPIKeyScopes = map[string]ApiKeyScope{
string(ApiKeyScopeAny): ApiKeyScopeAny,
string(ApiKeyScopeApplicationConnect): ApiKeyScopeApplicationConnect,
}

func ToAPIKeyScope(v string) (ApiKeyScope, error) {
scope, ok := validAPIKeyScopes[v]
if !ok {
return ApiKeyScope(""), xerrors.Errorf("invalid token scope: %s", v)
}

return scope, nil
}

func (s ApiKeyScope) ToRBAC() rbac.Scope {
switch {
case s == ApiKeyScopeAny:
switch s {
case ApiKeyScopeAny:
return rbac.ScopeAny
case s == ApiKeyScopeApplicationConnect:
case ApiKeyScopeApplicationConnect:
return rbac.ScopeApplicationConnect
default:
panic("developer error: unknown scope type " + string(s))
Expand Down