本文整理汇总了Golang中github.com/RangelReale/osin.OutputJSON函数的典型用法代码示例。如果您正苦于以下问题:Golang OutputJSON函数的具体用法?Golang OutputJSON怎么用?Golang OutputJSON使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了OutputJSON函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
port := flag.String("port", "14000", "Port number to listen on")
backend_url := flag.String("backend", "http://localhost:14001/authenticate", "Address of the authentication backend")
flag.Parse()
config := osin.NewServerConfig()
config.AllowGetAccessRequest = true
config.AllowClientSecretInParams = true
storage := NewInMemoryStorage()
load_clients(storage)
server := osin.NewServer(config, storage)
// Authorization code endpoint
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
resp := server.NewResponse()
if ar := server.HandleAuthorizeRequest(resp, r); ar != nil {
if !HandleLoginPage(*backend_url, resp, ar, w, r) {
return
}
ar.Authorized = true
server.FinishAuthorizeRequest(resp, r, ar)
}
if resp.IsError && resp.InternalError != nil {
fmt.Printf("ERROR: %s\n", resp.InternalError)
}
osin.OutputJSON(resp, w, r)
})
// Access token endpoint
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
resp := server.NewResponse()
if ar := server.HandleAccessRequest(resp, r); ar != nil {
ar.Authorized = true
server.FinishAccessRequest(resp, r, ar)
}
if resp.IsError && resp.InternalError != nil {
fmt.Printf("ERROR: (internal) %s\n", resp.InternalError)
}
osin.OutputJSON(resp, w, r)
})
// Information endpoint
http.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
resp := server.NewResponse()
if ir := server.HandleInfoRequest(resp, r); ir != nil {
server.FinishInfoRequest(resp, r, ir)
}
osin.OutputJSON(resp, w, r)
})
fs := http.FileServer(http.Dir("assets"))
http.Handle("/assets/", http.StripPrefix("/assets/", fs))
http.ListenAndServe(":"+*port, nil)
}
示例2: GET_info
func GET_info(w http.ResponseWriter, r *http.Request, s *osin.Server) {
resp := s.NewResponse()
if ir := s.HandleInfoRequest(resp, r); ir != nil {
s.FinishInfoRequest(resp, r, ir)
}
osin.OutputJSON(resp, w, r)
}
示例3: handleToken
func (s *server) handleToken(w http.ResponseWriter, r *http.Request) {
resp := s.oauthServer.NewResponse()
defer resp.Close()
if ar := s.oauthServer.HandleAccessRequest(resp, r); ar != nil {
switch ar.Type {
case osin.AUTHORIZATION_CODE:
ar.Authorized = true
case osin.REFRESH_TOKEN:
ar.Authorized = true
case osin.PASSWORD:
if ar.Username == "test" && ar.Password == "test" {
ar.Authorized = true
}
case osin.CLIENT_CREDENTIALS:
ar.Authorized = true
}
s.oauthServer.FinishAccessRequest(resp, r, ar)
}
if resp.IsError && resp.InternalError != nil {
fmt.Printf("ERROR: %s\n", resp.InternalError)
}
// if !resp.IsError {
// resp.Output["custom_parameter"] = 19923
// }
osin.OutputJSON(resp, w, r)
}
示例4: Info
// Info return the token's information via http
func Info(w http.ResponseWriter, r *http.Request) {
var (
server = OAuthComponent(r)
resp = server.NewResponse()
)
defer resp.Close()
if ir := server.HandleInfoRequest(resp, r); ir != nil {
// don't process if is already an error
if resp.IsError {
return
}
// output data
resp.Output["client_id"] = ir.AccessData.Client.GetId()
// resp.Output["access_token"] = ir.AccessData.AccessToken
resp.Output["token_type"] = server.Config.TokenType
resp.Output["expires_in"] = ir.AccessData.CreatedAt.Add(time.Duration(ir.AccessData.ExpiresIn)*time.Second).Sub(server.Now()) / time.Second
if ir.AccessData.RefreshToken != "" {
resp.Output["refresh_token"] = ir.AccessData.RefreshToken
}
if ir.AccessData.Scope != "" {
resp.Output["scope"] = ir.AccessData.Scope
}
if ir.AccessData.UserData != nil {
resp.Output["owner"] = ir.AccessData.UserData.(string)
}
}
//Right here retry with the session.
osin.OutputJSON(resp, w, r)
}
示例5: Token
// Token is the action of Get /oauth2/token
func Token() echo.HandlerFunc {
return func(c echo.Context) error {
resp := oauth.NewResponse()
defer resp.Close()
if ar := oauth.HandleAccessRequest(resp, c.Request()); ar != nil {
switch ar.Type {
case osin.AUTHORIZATION_CODE:
ar.Authorized = true
case osin.REFRESH_TOKEN:
ar.Authorized = true
case osin.PASSWORD:
if _, err := nerdz.Login(ar.Username, ar.Password); err == nil {
ar.Authorized = true
}
case osin.CLIENT_CREDENTIALS:
ar.Authorized = true
}
oauth.FinishAccessRequest(resp, c.Request(), ar)
}
if resp.IsError && resp.InternalError != nil {
return c.JSON(http.StatusInternalServerError, &rest.Response{
HumanMessage: "Internal Server error",
Message: resp.InternalError.Error(),
Status: http.StatusBadRequest,
Success: false,
})
}
return osin.OutputJSON(resp, c.Response(), c.Request())
}
}
示例6: Login
func (cz *Citizens) Login(auth *osin.Server) gin.HandlerFunc {
return func(c *gin.Context) {
// see https://github.com/RangelReale/osin/blob/master/example/complete/complete.go#L45
res := auth.NewResponse()
if aReq := auth.HandleAccessRequest(res, c.Request); aReq != nil {
// check username/password
user := &models.User{}
// find in DB
err := cz.Connection.Collection(COL_CITIZEN).FindOne(bson.M{"username": aReq.Username}, user)
// user found and has valid username/password
if err == nil && user.ValidCredentials(aReq.Username, aReq.Password) {
aReq.Authorized = true
// save user data along with the access token
aReq.UserData = gin.H{"username": user.UserName}
}
// creates the response automatically with error message and code
auth.FinishAccessRequest(res, c.Request, aReq)
}
if res.IsError && res.InternalError != nil {
fmt.Printf("ACCESS_ERROR: %s\n", res.InternalError)
}
osin.OutputJSON(res, c.Writer, c.Request)
}
}
示例7: NewOAuth2
func NewOAuth2(base string) *OAuth2 {
cfg := osin.NewServerConfig()
cfg.AllowGetAccessRequest = true
server := osin.NewServer(cfg, example.NewTestStorage())
funcauthorize := func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
resp := server.NewResponse()
defer resp.Close()
if ar := server.HandleAuthorizeRequest(resp, r); ar != nil {
if !example.HandleLoginPage(ar, w, r) {
return
}
ar.Authorized = true
server.FinishAuthorizeRequest(resp, r, ar)
}
if resp.IsError && resp.InternalError != nil {
fmt.Printf("ERROR: %s\n", resp.InternalError)
}
osin.OutputJSON(resp, w, r)
}
functoken := func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
resp := server.NewResponse()
defer resp.Close()
if ar := server.HandleAccessRequest(resp, r); ar != nil {
ar.Authorized = true
server.FinishAccessRequest(resp, r, ar)
}
if resp.IsError && resp.InternalError != nil {
fmt.Printf("ERROR: %s\n", resp.InternalError)
}
osin.OutputJSON(resp, w, r)
}
o := &OAuth2{
FuncAuthorize: funcauthorize,
FuncToken: functoken,
Router: httprouter.New(),
BaseURI: base,
}
o.InitRouter()
return o
}
示例8: HandleInfo
// Information endpoint
func (oauth *oAuthHandler) HandleInfo(w http.ResponseWriter, r *http.Request) {
server := oauth.server
resp := server.NewResponse()
if ir := server.HandleInfoRequest(resp, r); ir != nil {
server.FinishInfoRequest(resp, r, ir)
}
osin.OutputJSON(resp, w, r)
}
示例9: TokenHandler
func (h *Handler) TokenHandler(w http.ResponseWriter, r *http.Request) {
resp := h.server.NewResponse()
r.ParseForm()
defer resp.Close()
if ar := h.server.HandleAccessRequest(resp, r); ar != nil {
switch ar.Type {
case osin.AUTHORIZATION_CODE:
data, ok := ar.UserData.(string)
if !ok {
http.Error(w, fmt.Sprintf("Could not assert UserData to string: %v", ar.UserData), http.StatusInternalServerError)
return
}
var claims jwt.ClaimsCarrier
if err := json.Unmarshal([]byte(data), &claims); err != nil {
http.Error(w, fmt.Sprintf("Could not unmarshal UserData: %v", ar.UserData), http.StatusInternalServerError)
return
}
ar.UserData = jwt.NewClaimsCarrier(uuid.New(), claims.GetSubject(), h.Issuer, h.Audience, time.Now(), time.Now())
ar.Authorized = true
case osin.REFRESH_TOKEN:
data, ok := ar.UserData.(map[string]interface{})
if !ok {
http.Error(w, fmt.Sprintf("Could not assert UserData type: %v", ar.UserData), http.StatusInternalServerError)
return
}
claims := jwt.ClaimsCarrier(data)
ar.UserData = jwt.NewClaimsCarrier(uuid.New(), claims.GetSubject(), h.Issuer, h.Audience, time.Now(), time.Now())
ar.Authorized = true
case osin.PASSWORD:
// TODO if !ar.Client.isAllowedToAuthenticateUser
// TODO ... return
// TODO }
if user, err := h.authenticate(w, r, ar.Username, ar.Password); err == nil {
ar.UserData = jwt.NewClaimsCarrier(uuid.New(), user.GetID(), h.Issuer, h.Audience, time.Now(), time.Now())
ar.Authorized = true
}
case osin.CLIENT_CREDENTIALS:
ar.UserData = jwt.NewClaimsCarrier(uuid.New(), ar.Client.GetId(), h.Issuer, h.Audience, time.Now(), time.Now())
ar.Authorized = true
// TODO ASSERTION workflow http://leastprivilege.com/2013/12/23/advanced-oauth2-assertion-flow-why/
// TODO Since assertions are only a draft for now and there is no need for SAML or similar this is postponed.
//case osin.ASSERTION:
// if ar.AssertionType == "urn:hydra" && ar.Assertion == "osin.data" {
// ar.Authorized = true
// }
}
h.server.FinishAccessRequest(resp, r, ar)
}
if resp.IsError {
resp.StatusCode = http.StatusUnauthorized
}
osin.OutputJSON(resp, w, r)
}
示例10: GET_token
func GET_token(w http.ResponseWriter, r *http.Request, s *osin.Server) {
resp := s.NewResponse()
if ar := s.HandleAccessRequest(resp, r); ar != nil {
// always true
ar.Authorized = true
s.FinishAccessRequest(resp, r, ar)
}
osin.OutputJSON(resp, w, r)
}
示例11: handleInfo
func (s *Server) handleInfo(w http.ResponseWriter, r *http.Request) {
resp := s.server.NewResponse()
defer resp.Close()
if ir := s.server.HandleInfoRequest(resp, r); ir != nil {
s.server.FinishInfoRequest(resp, r, ir)
}
osin.OutputJSON(resp, w, r)
}
示例12: handleAuthorization
func handleAuthorization(w http.ResponseWriter, r *http.Request) {
resp := server.NewResponse()
defer resp.Close()
if ar := server.HandleAuthorizeRequest(resp, r); ar != nil {
if !example.HandleLoginPage(ar, w, r) {
return
}
ar.Authorized = true
scopes := make(map[string]bool)
for _, s := range strings.Fields(ar.Scope) {
scopes[s] = true
}
// If the "openid" connect scope is specified, attach an ID Token to the
// authorization response.
//
// The ID Token will be serialized and signed during the code for token exchange.
if scopes["openid"] {
// These values would be tied to the end user authorizing the client.
now := time.Now()
idToken := IDToken{
Issuer: issuer,
UserID: "id-of-test-user",
ClientID: ar.Client.GetId(),
Expiration: now.Add(time.Hour).Unix(),
IssuedAt: now.Unix(),
Nonce: r.URL.Query().Get("nonce"),
}
if scopes["profile"] {
idToken.Name = "Jane Doe"
idToken.GivenName = "Jane"
idToken.FamilyName = "Doe"
idToken.Locale = "us"
}
if scopes["email"] {
t := true
idToken.Email = "[email protected]"
idToken.EmailVerified = &t
}
// NOTE: The storage must be able to encode and decode this object.
ar.UserData = &idToken
}
server.FinishAuthorizeRequest(resp, r, ar)
}
if resp.IsError && resp.InternalError != nil {
log.Printf("internal error: %v", resp.InternalError)
}
osin.OutputJSON(resp, w, r)
}
示例13: Token
func (oauth *OAuth) Token(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Println("Token:\r\n")
resp := oauth.Server.NewResponse()
defer resp.Close()
if ar := oauth.Server.HandleAccessRequest(resp, r); ar != nil {
checkAccessRequest(oauth, w, r, ar)
oauth.Server.FinishAccessRequest(resp, r, ar)
}
osin.OutputJSON(resp, w, r)
}
示例14: info
// OAuth2 information endpoint
func (o *Api) info(w http.ResponseWriter, r *http.Request) {
log.Println(OAUTH2_API_PREFIX, "OAuthApi: info")
resp := o.oauthServer.NewResponse()
defer resp.Close()
if ir := o.oauthServer.HandleInfoRequest(resp, r); ir != nil {
o.oauthServer.FinishInfoRequest(resp, r, ir)
}
osin.OutputJSON(resp, w, r)
}
示例15: Info
// Info is the action of GET /oauth2/info
func Info() echo.HandlerFunc {
return func(c echo.Context) error {
resp := oauth.NewResponse()
defer resp.Close()
if ir := oauth.HandleInfoRequest(resp, c.Request()); ir != nil {
oauth.FinishInfoRequest(resp, c.Request(), ir)
}
return osin.OutputJSON(resp, c.Response(), c.Request())
}
}