Esempio n. 1
0
func (o *Oauth2) Request(db *database.Database, remoteState, remoteSecret,
	remoteCallback string, version int) (url string, err error) {

	coll := db.Tokens()
	state := utils.RandStr(64)

	url = o.conf.AuthCodeURL(state, oauth2.AccessTypeOffline,
		oauth2.ApprovalForce)
	if err != nil {
		err = &errortypes.UnknownError{
			errors.Wrap(err, "oauth.oauth2: Unknown api error"),
		}
		return
	}

	tokn := &Token{
		Id:             state,
		RemoteCallback: remoteCallback,
		RemoteState:    remoteState,
		RemoteSecret:   remoteSecret,
		Type:           o.Type,
		Version:        version,
	}
	err = coll.Insert(tokn)
	if err != nil {
		err = database.ParseError(err)
		return
	}

	return
}
Esempio n. 2
0
func (s *Saml) Request(db *database.Database, remoteState, remoteSecret,
	remoteCallback string) (resp *bytes.Buffer, err error) {

	coll := db.Tokens()
	state := utils.RandStr(64)

	req := s.provider.GetAuthnRequest()
	encodedReq, err := req.EncodedSignedString(s.provider.PrivateKeyPath)
	if err != nil {
		err = &SamlError{
			errors.Wrap(err, "saml: Encode error"),
		}
		return
	}

	data := struct {
		SsoUrl      string
		SAMLRequest string
		RelayState  string
	}{
		SsoUrl:      s.provider.IDPSSOURL,
		SAMLRequest: encodedReq,
		RelayState:  state,
	}

	respTemplate := template.New("saml")
	respTemplate, err = respTemplate.Parse(bindTemplate)
	if err != nil {
		err = &SamlError{
			errors.Wrap(err, "saml: Template parse error"),
		}
		return
	}

	tokn := &Token{
		Id:             state,
		RemoteCallback: remoteCallback,
		RemoteState:    remoteState,
		RemoteSecret:   remoteSecret,
		SsoUrl:         s.SsoUrl,
		IssuerUrl:      s.IssuerUrl,
		Cert:           s.Cert,
		Type:           "saml",
	}
	err = coll.Insert(tokn)
	if err != nil {
		err = database.ParseError(err)
		return
	}

	resp = &bytes.Buffer{}
	err = respTemplate.Execute(resp, data)
	if err != nil {
		err = &SamlError{
			errors.Wrap(err, "saml: Template execute error"),
		}
		return
	}

	return
}