workspaceAgentsExternalAuth returns an access token for a given URL or finds a provider by ID. @Summary Get workspace agent external auth @ID get-workspace-agent-external-auth @Security CoderSessionToken @Produce json @Tags Agents @Param match query string true "Match" @Param id query string true "
(rw http.ResponseWriter, r *http.Request)
| 1959 | // @Success 200 {object} agentsdk.ExternalAuthResponse |
| 1960 | // @Router /api/v2/workspaceagents/me/external-auth [get] |
| 1961 | func (api *API) workspaceAgentsExternalAuth(rw http.ResponseWriter, r *http.Request) { |
| 1962 | ctx := r.Context() |
| 1963 | query := r.URL.Query() |
| 1964 | gitRef := chatGitRef{ |
| 1965 | Branch: strings.TrimSpace(query.Get("git_branch")), |
| 1966 | RemoteOrigin: strings.TrimSpace(query.Get("git_remote_origin")), |
| 1967 | } |
| 1968 | if raw := strings.TrimSpace(query.Get("chat_id")); raw != "" { |
| 1969 | if parsed, err := uuid.Parse(raw); err == nil { |
| 1970 | gitRef.ChatID = parsed |
| 1971 | } |
| 1972 | } |
| 1973 | // Either match or configID must be provided! |
| 1974 | match := query.Get("match") |
| 1975 | if match == "" { |
| 1976 | // Support legacy agents! |
| 1977 | match = query.Get("url") |
| 1978 | } |
| 1979 | id := query.Get("id") |
| 1980 | if match == "" && id == "" { |
| 1981 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1982 | Message: "'url' or 'id' must be provided!", |
| 1983 | }) |
| 1984 | return |
| 1985 | } |
| 1986 | if match != "" && id != "" { |
| 1987 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1988 | Message: "'url' and 'id' cannot be provided together!", |
| 1989 | }) |
| 1990 | return |
| 1991 | } |
| 1992 | |
| 1993 | // listen determines if the request will wait for a |
| 1994 | // new token to be issued! |
| 1995 | listen := query.Has("listen") |
| 1996 | |
| 1997 | var externalAuthConfig *externalauth.Config |
| 1998 | for _, extAuth := range api.ExternalAuthConfigs { |
| 1999 | if extAuth.ID == id { |
| 2000 | externalAuthConfig = extAuth |
| 2001 | break |
| 2002 | } |
| 2003 | if match == "" || extAuth.Regex == nil { |
| 2004 | continue |
| 2005 | } |
| 2006 | matches := extAuth.Regex.MatchString(match) |
| 2007 | if !matches { |
| 2008 | continue |
| 2009 | } |
| 2010 | externalAuthConfig = extAuth |
| 2011 | } |
| 2012 | if externalAuthConfig == nil { |
| 2013 | detail := "External auth provider not found." |
| 2014 | if len(api.ExternalAuthConfigs) > 0 { |
| 2015 | regexURLs := make([]string, 0, len(api.ExternalAuthConfigs)) |
| 2016 | for _, extAuth := range api.ExternalAuthConfigs { |
| 2017 | if extAuth.Regex == nil { |
| 2018 | continue |
no test coverage detected