// auth is used to perform the initial authentication on connect
775017
func (c *RPCClient) auth(authKey string) error {
	header := requestHeader{
		Command: authCommand,
		Seq:     c.getSeq(),
	}
	req := authRequest{
		AuthKey: authKey,
	}
	return c.genericRPC(&header, &req, nil)
}
953805
func (c *Client) Connect() error {
	// An SSH client is represented with a ClientConn.
	//
	// To authenticate with the remote server you must pass at least one
	// implementation of AuthMethod via the Auth field in ClientConfig.
	var co *ssh.ClientConfig
	var err error

	if c.useAgent {
		co, err = AgentConfig(c.credentials.Username)
		if err != nil {
			return err
		}
	} else {
		co, err = KeyConfig(c.credentials.Username, c.key)
		if err != nil {
			return err
		}
	}

	client, err := ssh.Dial("tcp", net.JoinHostPort(c.addr, c.port), co)
	if err != nil {
		return err
	}

	c.client = client

	return nil
}
245070
func (a *accessTokenAuthenticator) Authenticate(_ string, token string, _ string) (*auth.Session, error) {
	at, err := a.findAccessToken(token)
	if err != nil {
		return nil, err
	}

	if at == nil {
		return nil, auth.ErrForbidden
	}

	session := &auth.Session{
		User:      at.User,
		ExpiresAt: at.ExpiresAt,
	}

	return session, nil
}
326974
func (sa *staticAuthenticator) Authenticate(user string, pass string) bool {
	password, found := sa.credentials[user]
	if !found {
		return false
	}
	return pass == password
}
1083176
func (b *basicAuth) requestAuth(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm=%q`, b.opts.Realm))
	b.opts.UnauthorizedHandler.ServeHTTP(w, r)
}
164900
func (g *gcm) auth(out, ciphertext, additionalData []byte, tagMask *[gcmBlockSize]byte) {
	var y gcmFieldElement
	g.update(&y, additionalData)
	g.update(&y, ciphertext)

	y.low ^= uint64(len(additionalData)) * 8
	y.high ^= uint64(len(ciphertext)) * 8

	g.mul(&y)

	putUint64(out, y.low)
	putUint64(out[8:], y.high)

	xorWords(out, out, tagMask[:])
}
285242
func (_ BasicAuth) Authenticate(config *Config) (*http.Client, APIResponse) {
	client := &http.Client{Transport: BasicAuthTransport{Config: config}}
	return client, apiResponse("basic auth", "", nil, nil)
}
983955
func (c *Client) Authenticate(username, password string) error {
	if username == "" {
		username = os.Getenv("VCLOUDAIR_USERNAME")
	}
	if password == "" {
		password = os.Getenv("VCLOUDAIR_PASSWORD")
	}

	r, _ := http.NewRequest("POST", c.VAEndpoint.String()+LoginPath, nil)
	r.Header.Set("Accept", JSONMimeV57)
	r.SetBasicAuth(username, password)

	resp, err := c.http.Do(r)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode/100 != 2 {
		return fmt.Errorf("Could not complete request with vca, because (status %d) %s\n", resp.StatusCode, resp.Status)
	}

	var result oAuthClient
	dec := json.NewDecoder(resp.Body)
	if err := dec.Decode(&result); err != nil {
		return err
	}
	result.AuthToken = resp.Header.Get("vchs-authorization")
	c.VAToken = result.AuthToken
	result.Config = c

	instances, err := result.instances()
	if err != nil {
		return err
	}

	var attrs *accountInstanceAttrs
	for _, inst := range instances {
		attrs = inst.Attrs()
		if attrs != nil && (c.Region == "" || strings.HasPrefix(inst.Region, c.Region)) {
			c.Region = inst.Region
			break
		}
	}

	if attrs == nil {
		return fmt.Errorf("unable to determine session url")
	}
	attrs.client = c

	return attrs.Authenticate(username, password)
}
685161
func (c *authenticatedConnection) SetAuthentication(auth driver.Authentication) (driver.Connection, error) {
	result, err := c.conn.SetAuthentication(auth)
	if err != nil {
		return nil, driver.WithStack(err)
	}
	return result, nil
}
824902
func (e *ECU) authConnect(ctx *ipc.Context) (*conn.Seesaw, error) {
	if ctx == nil {
		return nil, errors.New("context is nil")
	}
	authCtx, err := e.authenticate(ctx)
	if err != nil {
		return nil, fmt.Errorf("authentication failed: %v", err)
	}

	seesawConn, err := conn.NewSeesawIPC(authCtx)
	if err != nil {
		return nil, fmt.Errorf("failed to connect to engine: %v", err)
	}
	if err := seesawConn.Dial(e.cfg.EngineSocket); err != nil {
		return nil, fmt.Errorf("failed to connect to engine: %v", err)
	}

	return seesawConn, nil
}
271215
func (si *SharedInfo) Auth(passwd string) error {
	if si.SharedURL == "" {
		return ErrSharedInfoNotSetSharedURL
	}

	si.lazyInit()

	// 不自动跳转
	si.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
		if strings.Contains(req.URL.String(), "surl=") {
			return http.ErrUseLastResponse
		}

		if len(via) >= 10 {
			return errors.New("stopped after 10 redirects")
		}
		return nil
	}

	resp, err := si.Client.Req("GET", si.SharedURL, nil, nil)
	if resp != nil {
		defer resp.Body.Close()
	}
	if err != nil {
		return err
	}

	switch resp.StatusCode / 100 {
	case 3: // 需要输入提取密码
		locURL, err := resp.Location()
		if err != nil {
			return fmt.Errorf("检测提取码, 提取 Location 错误, %s", err)
		}

		// 验证提取密码
		body, err := si.Client.Fetch("POST", "https://pan.baidu.com/share/verify?"+locURL.RawQuery, map[string]string{
			"pwd":       passwd,
			"vcode":     "",
			"vcode_str": "",
		}, map[string]string{
			"Content-Type": "application/x-www-form-urlencoded",
			"Referer":      "https://pan.baidu.com/",
		})

		if err != nil {
			return fmt.Errorf("验证提取密码网络错误, %s", err)
		}

		jsonData := &RemoteErrInfo{}

		err = jsoniter.Unmarshal(body, jsonData)
		if err != nil {
			return fmt.Errorf("验证提取密码, json数据解析失败, %s", err)
		}

		switch jsonData.ErrNo {
		case 0: // 密码正确
			break
		default:
			return fmt.Errorf("验证提取密码遇到错误, %s", jsonData)
		}
	case 4, 5:
		return fmt.Errorf(resp.Status)
	}

	return nil
}
445105
func (ras RouteAuthenticators) Authenticate(req *http.Request, route *MatchedRoute) (bool, interface{}, error) {
	var lastError error
	var allowsAnon bool
	var anonAuth RouteAuthenticator

	for _, ra := range ras {
		if ra.AllowsAnonymous() {
			anonAuth = ra
			allowsAnon = true
			continue
		}
		applies, usr, err := ra.Authenticate(req, route)
		if !applies || err != nil || usr == nil {
			if err != nil {
				lastError = err
			}
			continue
		}
		return applies, usr, nil
	}

	if allowsAnon && lastError == nil {
		route.Authenticator = &anonAuth
		return true, nil, lastError
	}
	return lastError != nil, nil, lastError
}
1265576
func (p *Provider) BeginAuth(state string) (goth.Session, error) {
	authURL := p.config.AuthCodeURL(state)

	return &Session{
		AuthURL: authURL,
	}, nil
}
529748
func (cm *connectMethod) proxyAuth() string {
	if cm.proxyURL == nil {
		return ""
	}
	if u := cm.proxyURL.User; u != nil {
		return "Basic " + base64.URLEncoding.EncodeToString([]byte(u.String()))
	}
	return ""
}
727411
func (m UsersAPIAuthMethod) Authenticate(c context.Context, r *http.Request) (*auth.User, error) {
	u := user.Current(c)
	if u == nil {
		return nil, nil
	}
	id, err := identity.MakeIdentity("user:" + u.Email)
	if err != nil {
		return nil, err
	}
	return &auth.User{
		Identity:  id,
		Superuser: u.Admin,
		Email:     u.Email,
	}, nil
}
807225
func (c *agencyConnection) SetAuthentication(auth driver.Authentication) (driver.Connection, error) {
	c.mutex.Lock()
	defer c.mutex.Unlock()

	newConnections := make([]driver.Connection, len(c.connections))
	for i, x := range c.connections {
		xAuth, err := x.SetAuthentication(auth)
		if err != nil {
			return nil, driver.WithStack(err)
		}
		newConnections[i] = xAuth
	}
	c.connections = newConnections
	c.auth = auth
	return c, nil
}
591703
func (c *Connection) Authenticated() bool {
	c.authLock.Lock()
	defer c.authLock.Unlock()
	return c.authenticated()
}