MCPcopy
hub / github.com/tinode/chat / validatedCreds

Function validatedCreds

server/user.go:408–482  ·  view source on GitHub ↗

validatedCreds returns the list of validated credentials including those validated in this call. Returns all validated methods including those validated earlier and now. Returns either a full set of tags or nil for tags if tags are unchanged.

(uid types.Uid, authLvl auth.Level, creds []MsgCredClient,
	errorOnFail bool)

Source from the content-addressed store, hash-verified

406// Returns all validated methods including those validated earlier and now.
407// Returns either a full set of tags or nil for tags if tags are unchanged.
408func validatedCreds(uid types.Uid, authLvl auth.Level, creds []MsgCredClient,
409 errorOnFail bool) ([]string, []string, error) {
410 // Check if credential validation is required.
411 if len(globals.authValidators[authLvl]) == 0 {
412 return nil, nil, nil
413 }
414
415 // Get all validated methods
416 allCreds, err := store.Users.GetAllCreds(uid, "", true)
417 if err != nil {
418 return nil, nil, err
419 }
420
421 methods := make(map[string]struct{})
422 for i := range allCreds {
423 methods[allCreds[i].Method] = struct{}{}
424 }
425
426 // Add credentials which are validated in this call.
427 // Unknown validators are removed.
428 creds = normalizeCredentials(creds, false)
429 var tagsToAdd []string
430 for i := range creds {
431 cr := &creds[i]
432 if cr.Response == "" {
433 // Ignore empty response.
434 continue
435 }
436
437 vld := store.Store.GetValidator(cr.Method) // No need to check for nil, unknown methods are removed earlier.
438 value, err := vld.Check(uid, cr.Response)
439
440 if err != nil {
441 // Check failed.
442 if storeErr, ok := err.(types.StoreError); ok && storeErr == types.ErrCredentials {
443 if errorOnFail {
444 // Report invalid response.
445 return nil, nil, types.ErrInvalidResponse
446 }
447 // Skip invalid response. Keep credential unvalidated.
448 continue
449 }
450 // Actual error. Report back.
451 return nil, nil, err
452 }
453
454 // Check did not return an error: the request was successfully validated.
455 methods[cr.Method] = struct{}{}
456
457 // Add validated credential to user's tags.
458 if globals.validators[cr.Method].addToTags {
459 tagsToAdd = append(tagsToAdd, cr.Method+":"+value)
460 }
461 }
462
463 var tags []string
464 if len(tagsToAdd) > 0 {
465 // Save update to tags

Callers 2

loginMethod · 0.85
replySetCredMethod · 0.85

Calls 6

normalizeCredentialsFunction · 0.85
PrintlnMethod · 0.80
GetAllCredsMethod · 0.65
GetValidatorMethod · 0.65
CheckMethod · 0.65
UpdateTagsMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…