// 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()) } }
func TestLogin(t *testing.T) { if _, e := nerdz.Login("1", "adminadmin"); e != nil { t.Fatalf("Login using ID and password shold work but got: %s", e.Error()) } if _, e := nerdz.Login("*****@*****.**", "adminadmin"); e != nil { t.Fatalf("Login using email and password shold work but got: %s", e.Error()) } if _, e := nerdz.Login("admin", "adminadmin"); e != nil { t.Fatalf("Login using username and password shold work but got: %s", e.Error()) } if _, e := nerdz.Login("BANANA", "adminadmin"); e == nil { t.Fatalf("Login using a wrong username and passowrd shold fail. But it worked") } }