ajout base de donnees pour param et oauth
This commit is contained in:
5
main.go
5
main.go
@@ -5,6 +5,8 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/ldrogou/goauth20/store"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -18,7 +20,8 @@ func main() {
|
||||
|
||||
func run() error {
|
||||
srv := newServer()
|
||||
srv.store = &dbStore{}
|
||||
srv.store = &store.DbStore{}
|
||||
|
||||
err := srv.store.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
15
model/oauth.go
Normal file
15
model/oauth.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package model
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Oauth struct {
|
||||
ID int64 `db:"id"`
|
||||
AccessToken string `db:"access_token"`
|
||||
ExpireIN int `db:"expire_in"`
|
||||
RefreshToken string `db:"refreh_token"`
|
||||
}
|
||||
|
||||
func (o Oauth) String() string {
|
||||
return fmt.Sprintf("id=%v, accessToken=%v, expireIN=%v, refreshToken=%v",
|
||||
o.ID, o.AccessToken, o.ExpireIN, o.RefreshToken)
|
||||
}
|
||||
16
model/param.go
Normal file
16
model/param.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package model
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Param struct {
|
||||
ID int64 `db:"id"`
|
||||
Domaine string `db:"domaine"`
|
||||
ClientID string `db:"client_id"`
|
||||
ClientSecret string `db:"client_secret"`
|
||||
GrantType string `db:"grant_type"`
|
||||
}
|
||||
|
||||
func (p Param) String() string {
|
||||
return fmt.Sprintf("id=%v, title=%v, releaseDate=%v, duration=%v, trailerURL=%v",
|
||||
p.ID, p.Domaine, p.ClientID, p.ClientSecret, p.GrantType)
|
||||
}
|
||||
@@ -104,19 +104,22 @@ func (s *server) handleLocal() http.HandlerFunc {
|
||||
func (s *server) handleOAuth20() http.HandlerFunc {
|
||||
return func(rw http.ResponseWriter, r *http.Request) {
|
||||
|
||||
domain := r.FormValue("domain")
|
||||
clientID := r.FormValue("clientId")
|
||||
scopes := r.FormValue("scopes")
|
||||
currentCompany := r.FormValue("currentCompany")
|
||||
if len(currentCompany) == 0 {
|
||||
currentCompany = "false"
|
||||
d := r.FormValue("domain")
|
||||
ci := r.FormValue("clientId")
|
||||
s := r.FormValue("scopes")
|
||||
cc := r.FormValue("currentCompany")
|
||||
if len(cc) == 0 {
|
||||
cc = "false"
|
||||
} else {
|
||||
currentCompany = "true"
|
||||
cc = "true"
|
||||
}
|
||||
|
||||
log.Println(currentCompany)
|
||||
redirecthttp := "https://" + domain + "/entreprise-partenaire/authorize?client_id=" + clientID + "&scope=" + scopes + "¤t_company=" + currentCompany + "&redirect_uri=http://localhost:8080/oauth/redirect&abort_uri=http://localhost:8080/index"
|
||||
http.Redirect(rw, r, redirecthttp, http.StatusMovedPermanently)
|
||||
rhttp := "https://" + d + "/entreprise-partenaire/authorize?client_id=" + ci +
|
||||
"&scope=" + s +
|
||||
"¤t_company=" + cc +
|
||||
"&redirect_uri=http://localhost:8080/oauth/redirect" +
|
||||
"&abort_uri=http://localhost:8080/index"
|
||||
http.Redirect(rw, r, rhttp, http.StatusMovedPermanently)
|
||||
|
||||
}
|
||||
|
||||
@@ -147,8 +150,6 @@ func (s *server) handleRedirect() http.HandlerFunc {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("response Status:", resp.Status)
|
||||
fmt.Println("response Headers:", resp.Header)
|
||||
var t map[string]interface{}
|
||||
// here's the trick
|
||||
json.NewDecoder(resp.Body).Decode(&t)
|
||||
|
||||
20
server.go
20
server.go
@@ -10,12 +10,14 @@ import (
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/ldrogou/goauth20/model"
|
||||
"github.com/ldrogou/goauth20/store"
|
||||
templateoauth "github.com/ldrogou/goauth20/templateOAuth"
|
||||
)
|
||||
|
||||
type server struct {
|
||||
router *mux.Router
|
||||
store Store
|
||||
store store.Store
|
||||
}
|
||||
|
||||
//File structure du fichier
|
||||
@@ -70,7 +72,7 @@ func (s *server) responseFile(rw http.ResponseWriter, _ *http.Request, data inte
|
||||
}
|
||||
|
||||
//t := template.New("mon template")
|
||||
tem, err := template.New("Resulta").Parse(templateoauth.Resultat)
|
||||
t, err := template.New("Resulta").Parse(templateoauth.Resultat)
|
||||
if err != nil {
|
||||
return fmt.Errorf("erreur suivante %v", err)
|
||||
}
|
||||
@@ -82,7 +84,19 @@ func (s *server) responseFile(rw http.ResponseWriter, _ *http.Request, data inte
|
||||
Sign: tableau[2],
|
||||
}
|
||||
|
||||
err = tem.Execute(rw, f)
|
||||
o := &model.Oauth{
|
||||
ID: 0,
|
||||
AccessToken: tokenVal,
|
||||
ExpireIN: 180,
|
||||
RefreshToken: "eeeee",
|
||||
}
|
||||
err = s.store.CreateOauth(o)
|
||||
if err != nil {
|
||||
fmt.Printf("erreur suivante %v", err)
|
||||
}
|
||||
|
||||
log.Println("ezdzedezd")
|
||||
err = t.Execute(rw, f)
|
||||
if err != nil {
|
||||
return fmt.Errorf("erreur suivante %v", err)
|
||||
}
|
||||
|
||||
43
store.go
43
store.go
@@ -1,43 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type Store interface {
|
||||
Open() error
|
||||
Close() error
|
||||
}
|
||||
|
||||
type dbStore struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
var schema = `
|
||||
CREATE TABLE IF NOT EXISTS auth
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
access_token TEXT,
|
||||
expire_in TEXT,
|
||||
refreh_token TEXT
|
||||
)
|
||||
`
|
||||
|
||||
func (store *dbStore) Open() error {
|
||||
db, err := sqlx.Connect("sqlite3", "auth.db")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Println("Connected db")
|
||||
db.MustExec(schema)
|
||||
|
||||
store.db = db
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *dbStore) Close() error {
|
||||
return store.db.Close()
|
||||
}
|
||||
126
store/store.go
Normal file
126
store/store.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/ldrogou/goauth20/model"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type Store interface {
|
||||
Open() error
|
||||
Close() error
|
||||
|
||||
GetOauth() (*model.Oauth, error)
|
||||
CreateOauth(m *model.Oauth) error
|
||||
DeleteOauth() error
|
||||
|
||||
GetParam() (*model.Param, error)
|
||||
CreateParam(m *model.Param) error
|
||||
DeleteParam() error
|
||||
}
|
||||
|
||||
type DbStore struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
var schemaAuth = `
|
||||
CREATE TABLE IF NOT EXISTS oauth
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
access_token TEXT,
|
||||
expire_in INTEGER,
|
||||
refresh_token TEXT
|
||||
)
|
||||
`
|
||||
|
||||
var schemaParam = `
|
||||
CREATE TABLE IF NOT EXISTS param
|
||||
(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
domaine TEXT,
|
||||
client_id TEXT,
|
||||
client_secret TEXT,
|
||||
grant_type TEXT
|
||||
)
|
||||
`
|
||||
|
||||
func (store *DbStore) Open() error {
|
||||
db, err := sqlx.Connect("sqlite3", "auth.db")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Println("Connected db")
|
||||
db.MustExec(schemaAuth)
|
||||
db.MustExec(schemaParam)
|
||||
|
||||
store.db = db
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *DbStore) Close() error {
|
||||
return store.db.Close()
|
||||
}
|
||||
|
||||
func (store *DbStore) GetOauth() (*model.Oauth, error) {
|
||||
var oauth = &model.Oauth{}
|
||||
err := store.db.Get(oauth, "SELECT * FROM oauth")
|
||||
if err != nil {
|
||||
return oauth, err
|
||||
}
|
||||
return oauth, nil
|
||||
}
|
||||
|
||||
func (store *DbStore) CreateOauth(o *model.Oauth) error {
|
||||
res, err := store.db.Exec("INSERT INTO oauth (access_token, expire_in, refresh_token) VALUES (?, ?, ?)",
|
||||
o.AccessToken, o.ExpireIN, o.RefreshToken)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.ID, err = res.LastInsertId()
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func (store *DbStore) DeleteOauth() error {
|
||||
_, err := store.db.Exec("DELETE TABLE oauth", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
}
|
||||
func (store *DbStore) GetParam() (*model.Param, error) {
|
||||
var param = &model.Param{}
|
||||
err := store.db.Get(param, "SELECT * FROM param")
|
||||
if err != nil {
|
||||
return param, err
|
||||
}
|
||||
return param, nil
|
||||
}
|
||||
|
||||
func (store *DbStore) CreateParam(p *model.Param) error {
|
||||
res, err := store.db.Exec("INSERT INTO param (domaine, client_id, client_secret, grant_type) VALUES (?, ?, ?, ?)",
|
||||
p.Domaine, p.ClientID, p.ClientSecret, p.GrantType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.ID, err = res.LastInsertId()
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func (store *DbStore) DeleteParam() error {
|
||||
_, err := store.db.Exec("DELETE TABLE param", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
}
|
||||
51
store/storeoauth.go
Normal file
51
store/storeoauth.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
/**
|
||||
type StoreOauth interface {
|
||||
GetOauth() (*model.Oauth, error)
|
||||
CreateOauth(m *model.Oauth) error
|
||||
DeleteOauth() error
|
||||
}
|
||||
|
||||
func (store *DbStore) GetOauth() (*model.Oauth, error) {
|
||||
var oauth = &model.Oauth{}
|
||||
err := store.db.Get(oauth, "SELECT * FROM oauth")
|
||||
if err != nil {
|
||||
return oauth, err
|
||||
}
|
||||
return oauth, nil
|
||||
}
|
||||
|
||||
func (store *DbStore) CreateOauth(o *model.Oauth) error {
|
||||
log.Println("je suis ici")
|
||||
log.Println(store.db.Ping())
|
||||
log.Println("je suis ici -------")
|
||||
log.Printf("la valeur de o %v \n", o)
|
||||
log.Println("je suis ici =======")
|
||||
res, err := store.db.Exec("INSERT INTO oauth (access_token, expire_in, refresh_token) VALUES (?, ?, ?)",
|
||||
o.AccessToken, o.ExpireIN, o.RefreshToken)
|
||||
log.Println("je suis ici @@@@@@@@")
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.ID, err = res.LastInsertId()
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func (store *DbStore) DeleteOauth() error {
|
||||
_, err := store.db.Exec("DELETE TABLE oauth", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
}
|
||||
**/
|
||||
44
store/storeparam.go
Normal file
44
store/storeparam.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
/**
|
||||
type StoreParam interface {
|
||||
GetParam() (*model.Param, error)
|
||||
CreateParam(m *model.Param) error
|
||||
DeleteParam() error
|
||||
}
|
||||
|
||||
func (store *DbStore) GetParam() (*model.Param, error) {
|
||||
var param = &model.Param{}
|
||||
err := store.db.Get(param, "SELECT * FROM param")
|
||||
if err != nil {
|
||||
return param, err
|
||||
}
|
||||
return param, nil
|
||||
}
|
||||
|
||||
func (store *DbStore) CreateParam(p *model.Param) error {
|
||||
res, err := store.db.Exec("INSERT INTO param (domaine, client_id, client_secret, grant_type) VALUES (?, ?, ?, ?)",
|
||||
p.Domaine, p.ClientID, p.ClientSecret, p.GrantType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.ID, err = res.LastInsertId()
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func (store *DbStore) DeleteParam() error {
|
||||
_, err := store.db.Exec("DELETE TABLE param", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
}
|
||||
**/
|
||||
@@ -33,15 +33,15 @@ var Resultat = `<!DOCTYPE html>
|
||||
|
||||
<ul class="collapsible collapsible-accordion">
|
||||
<li>
|
||||
<div class="collapsible-header"><i class="material-icons">filter_drama</i>header</div>
|
||||
<div class="collapsible-header"><i class="material-icons">account_box</i>header</div>
|
||||
<div class="collapsible-body" ><pre id="header"></pre></div>
|
||||
</li>
|
||||
<li class="active">
|
||||
<div class="collapsible-header"><i class="material-icons">place</i>payload</div>
|
||||
<div class="collapsible-header"><i class="material-icons">code</i>payload</div>
|
||||
<div class="collapsible-body"><pre id="payload"></pre></div>
|
||||
</li>
|
||||
<li>
|
||||
<div class="collapsible-header"><i class="material-icons">whatshot</i>signature</div>
|
||||
<div class="collapsible-header"><i class="material-icons">border_color</i>signature</div>
|
||||
<div class="collapsible-body"><span>{{.Sign}}</span></div>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
Reference in New Issue
Block a user