// getAuthRequest assembles the request to get the authenticated cookie
514262
func (t *CookieAuthTransport) buildAuthRequest() (*http.Request, error) {
	body := struct {
		Username string `json:"username"`
		Password string `json:"password"`
	}{
		t.Username,
		t.Password,
	}

	b := new(bytes.Buffer)
	json.NewEncoder(b).Encode(body)

	req, err := http.NewRequest("POST", t.AuthURL, b)
	if err != nil {
		return nil, err
	}

	req.Header.Set("Content-Type", "application/json")
	return req, nil
}
657851
func (ts TokenSource) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
	token, err := ts.Token()
	if err != nil {
		return nil, err
	}
	return map[string]string{
		"authorization": token.Type() + " " + token.AccessToken,
	}, nil
}
1178926
func (c *cookies) Get(key string) *http.Cookie {
	ck, err := c.req.Cookie(key)
	if err != nil {
		return nil
	}
	return ck
}
984761
func webRequestAuthenticate(req *http.Request) (jwtgo.StandardClaims, bool, error) {
	var claims = jwtgo.StandardClaims{}
	tokStr, err := jwtreq.AuthorizationHeaderExtractor.ExtractToken(req)
	if err != nil {
		if err == jwtreq.ErrNoTokenInRequest {
			return claims, false, errNoAuthToken
		}
		return claims, false, err
	}
	jwtToken, err := parseJWTWithClaims(tokStr, &claims)
	if err != nil {
		return claims, false, err
	}
	if !jwtToken.Valid {
		return claims, false, errAuthentication
	}
	owner := claims.Subject == globalServerConfig.GetCredential().AccessKey
	return claims, owner, nil
}
1017825
func (ua *UserAgentAuthentication) Request(ctx context.Context, req *http.Request) (*http.Response, error) {
	// nothing to do gtfo
	if ua.UserAgentPassword == "" {
		return ua.Requester(ctx, req)
	}
	//
	retsVersion := ""
	if ua.GetRETSVersion != nil {
		retsVersion = ua.GetRETSVersion(req)
	}
	// we generate this and set it in the headers
	requestID := ""
	if ua.CreateRequestID != nil {
		requestID = ua.CreateRequestID(req)
		req.Header.Set(RETSRequestID, requestID)
	}
	// need to extract this from context that we may not have at this level
	sessionID := ""
	if ua.GetSessionID != nil {
		sessionID = ua.GetSessionID(req)
	}
	uaAuthHeader := ua.header(requestID, sessionID, retsVersion)
	// this will replace an existing value
	req.Header.Set(RETSUAAuth, uaAuthHeader)
	return ua.Requester(ctx, req)
}
1125231
func (c ClientCookies) GetToken(r *http.Request) string {
	cookie, err := r.Cookie(c.Name)
	if err != nil {
		return ""
	}
	return cookie.Value
}
1179401
func getAuthToken(u *dt.User) (header *Header, authToken string, err error) {
	scopes := []string{}
	if u.Admin {
		scopes = append(scopes, "admin")
	}
	header = &Header{
		ID:       u.ID,
		Email:    u.Email,
		Scopes:   scopes,
		IssuedAt: time.Now().Unix(),
	}
	byt, err := json.Marshal(header)
	if err != nil {
		return nil, "", err
	}
	hash := hmac.New(sha512.New, []byte(os.Getenv("ABOT_SECRET")))
	_, err = hash.Write(byt)
	if err != nil {
		return nil, "", err
	}
	authToken = base64.StdEncoding.EncodeToString(hash.Sum(nil))
	return header, authToken, nil
}
185233
func (service *Service) getSessionKey(request *http.Request) (sessionKey string, err error) {
	loginSession, err := service.GetSession(request, SessionLogin, "loginsession")
	if err != nil {
		log.Error(err)
		return
	}
	savedSessionKey := loginSession.Values["sessionkey"]
	if savedSessionKey != nil {
		sessionKey, _ = savedSessionKey.(string)
	}
	return
}
1034041
func (m *middleware) authenticate(r *http.Request) (context.Context, *Error) {
	var h = r.Header.Get(authorizationHeader)
	if len(h) > 0 {
		return m.validateHeader(r, h)
	}

	cookie, err := r.Cookie(m.config.TokenCookie)
	if err == nil && cookie != nil {
		return m.validateJWT(r, cookie.Value)
	}

	// from query string
	token := r.URL.Query().Get(m.config.TokenKey)
	if len(token) > 0 {
		return m.validateJWT(r, token)
	}

	return nil, ErrBadAuthorizationHeader
}
123403
func getRequestingClientFromRequest(r *http.Request, w http.ResponseWriter, organizationGlobalID string, allowOnWebsite bool) (string, bool) {
	requestingClient, validClient := context.Get(r, "client_id").(string)
	if !validClient {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return requestingClient, false
	}
	if requestingClient == "itsyouonline" {
		if allowOnWebsite {
			requestingClient = organizationGlobalID
		} else {
			// This should never happen as the oauth 2  middleware should give a 403
			writeErrorResponse(w, http.StatusBadRequest, "This api call is not available when logged in via the website")
			return requestingClient, false
		}
	} else if requestingClient != organizationGlobalID {
		writeErrorResponse(w, http.StatusForbidden, "unauthorized_organization")
		return requestingClient, false
	}
	return requestingClient, true
}
1074486
func AuthHTTPRequest(req *http.Request, info *Info) error {
	var tag string
	if info.Tag != nil {
		tag = info.Tag.String()
	}
	return authHTTPRequest(req, tag, info.Password, info.Nonce, info.Macaroons)
}
259672
func (r *apiHandler) GetAuthTag() names.Tag {
	if r.entity == nil {
		return nil
	}
	return r.entity.Tag()
}
879155
func (m *Module) Authenticate(rw http.ResponseWriter, req *http.Request) (handled bool, userToken *string, err error) {
	session, err := m.getSessionFromRequest(req)
	// getSessionFromRequest returns an error if there was a cookie
	// but it was invalid, so consider the request handled
	if err != nil {
		return true, nil, err
	}
	// no error and no session, so no cookie
	if session == nil {
		return false, nil, nil
	}
	return true, &session.ID, nil
}
553125
func (s *Service) AuthenticateRequest(r *http.Request) error {
	if s.secretKey == nil {
		return fmt.Errorf("service not loaded with key.")
	}
	return s.AuthenticateRequestWithKey(r, s.secretKey)
}
530706
func (a authCookieSha1) getCookie(context, id []byte) []byte {
	file, err := os.Open(a.home + "/.dbus-keyrings/" + string(context))
	if err != nil {
		return nil
	}
	defer file.Close()
	rd := bufio.NewReader(file)
	for {
		line, err := rd.ReadBytes('\n')
		if err != nil {
			return nil
		}
		line = line[:len(line)-1]
		b := bytes.Split(line, []byte{' '})
		if len(b) != 3 {
			return nil
		}
		if bytes.Equal(b[0], id) {
			return b[2]
		}
	}
}
717991
func (s *CookieStore) Get(req *http.Request, name string) (session *Session, err error) {
	cookie, err := req.Cookie(name)
	if err == nil {
		session = s.New(name)
		err = securecookie.DecodeMulti(name, cookie.Value, &session.Values, s.Codecs...)
	}
	return session, err
}
19404
func getSessionToken(r *http.Request) (token string) {
	token = r.Header.Get("X-Amz-Security-Token")
	if token != "" {
		return token
	}
	return r.URL.Query().Get("X-Amz-Security-Token")
}