MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / authorize

Method authorize

db/functions/function.go:264–299  ·  view source on GitHub ↗

Authorizes a User against the function config's Allow object: - The user's name must be contained in Users, OR - The user must have a role contained in Roles, OR - The user must have access to a channel contained in Channels. In Roles and Channels, patterns of the form `${param}` are expanded using

(user auth.User, args map[string]any)

Source from the content-addressed store, hash-verified

262// - The user must have access to a channel contained in Channels.
263// In Roles and Channels, patterns of the form `${param}` are expanded using `args` and `user`.
264func (fn *functionImpl) authorize(user auth.User, args map[string]any) error {
265 allow := fn.Allow
266 if user == nil {
267 return nil // User is admin
268 } else if allow != nil { // No Allow object means admin-only
269 if allow.Users.Contains(user.Name()) {
270 return nil // User is explicitly allowed
271 }
272 userRoles := user.RoleNames()
273 for _, rolePattern := range allow.Roles {
274 if role, err := expandPattern(rolePattern, args, user); err != nil {
275 return err
276 } else if userRoles.Contains(role) {
277 return nil // User has one of the allowed roles
278 }
279 }
280 // Check if the user has access to one of the given channels.
281 for _, channelPattern := range allow.Channels {
282 if channelPattern == channels.AllChannelWildcard {
283 return nil
284 }
285 channel, err := expandPattern(channelPattern, args, user)
286 if err != nil {
287 return err
288 }
289 canSee, err := user.CanSeeCollectionChannel(base.DefaultScope, base.DefaultCollection, channel)
290 if err != nil {
291 return err
292 }
293 if canSee {
294 return nil // User has access to one of the allowed channels
295 }
296 }
297 }
298 return user.UnauthError(base.HTTPErrorf(http.StatusForbidden, "You are not allowed to call %s %q", fn.typeName, fn.name))
299}
300
301// Expands patterns of the form `${param}` in `pattern`, looking up each such
302// `param` in the `args` map and substituting its value.

Callers 1

InvokeMethod · 0.95

Calls 7

HTTPErrorfFunction · 0.92
expandPatternFunction · 0.85
ContainsMethod · 0.65
NameMethod · 0.65
RoleNamesMethod · 0.65
UnauthErrorMethod · 0.65

Tested by

no test coverage detected