validateApplicationService checks if a provided application service token corresponds to one that is registered. If so, then it checks if the desired username is within that application service's namespace. As long as these two requirements are met, no error will be returned.
( cfg *config.ClientAPI, username string, accessToken string, )
| 481 | // username is within that application service's namespace. As long as these |
| 482 | // two requirements are met, no error will be returned. |
| 483 | func validateApplicationService( |
| 484 | cfg *config.ClientAPI, |
| 485 | username string, |
| 486 | accessToken string, |
| 487 | ) (string, *util.JSONResponse) { |
| 488 | // Check if the token if the application service is valid with one we have |
| 489 | // registered in the config. |
| 490 | var matchedApplicationService *config.ApplicationService |
| 491 | for _, appservice := range cfg.Derived.ApplicationServices { |
| 492 | if appservice.ASToken == accessToken { |
| 493 | matchedApplicationService = &appservice |
| 494 | break |
| 495 | } |
| 496 | } |
| 497 | if matchedApplicationService == nil { |
| 498 | return "", &util.JSONResponse{ |
| 499 | Code: http.StatusUnauthorized, |
| 500 | JSON: jsonerror.UnknownToken("Supplied access_token does not match any known application service"), |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | userID := userutil.MakeUserID(username, cfg.Matrix.ServerName) |
| 505 | |
| 506 | // Ensure the desired username is within at least one of the application service's namespaces. |
| 507 | if !UserIDIsWithinApplicationServiceNamespace(cfg, userID, matchedApplicationService) { |
| 508 | // If we didn't find any matches, return M_EXCLUSIVE |
| 509 | return "", &util.JSONResponse{ |
| 510 | Code: http.StatusBadRequest, |
| 511 | JSON: jsonerror.ASExclusive(fmt.Sprintf( |
| 512 | "Supplied username %s did not match any namespaces for application service ID: %s", username, matchedApplicationService.ID)), |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | // Check this user does not fit multiple application service namespaces |
| 517 | if UsernameMatchesMultipleExclusiveNamespaces(cfg, userID) { |
| 518 | return "", &util.JSONResponse{ |
| 519 | Code: http.StatusBadRequest, |
| 520 | JSON: jsonerror.ASExclusive(fmt.Sprintf( |
| 521 | "Supplied username %s matches multiple exclusive application service namespaces. Only 1 match allowed", username)), |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // Check username application service is trying to register is valid |
| 526 | if err := validateApplicationServiceUsername(username); err != nil { |
| 527 | return "", err |
| 528 | } |
| 529 | |
| 530 | // No errors, registration valid |
| 531 | return matchedApplicationService.ID, nil |
| 532 | } |
| 533 | |
| 534 | // Register processes a /register request. |
| 535 | // http://matrix.org/speculator/spec/HEAD/client_server/unstable.html#post-matrix-client-unstable-register |