github.com/wanliu/go-oauth2-server@v0.0.0-20180817021415-f928fa1580df/web/client.go (about)

     1  package web
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  
     8  	"github.com/gorilla/mux"
     9  	"github.com/wanliu/go-oauth2-server/form"
    10  	"github.com/wanliu/util/rand"
    11  )
    12  
    13  func (s *Service) listClient(w http.ResponseWriter, r *http.Request) {
    14  	sessionService, err := getSessionService(r)
    15  	if err != nil {
    16  		http.Error(w, err.Error(), http.StatusInternalServerError)
    17  		return
    18  	}
    19  
    20  	session, err := sessionService.GetUserSession()
    21  	currentUser, err := s.GetOauthService().FindUserByUsername(session.Username)
    22  	if err != nil {
    23  		http.Error(w, err.Error(), http.StatusInternalServerError)
    24  		return
    25  	}
    26  
    27  	clients, err := s.GetOauthService().ListClientByUserID(currentUser.ID, 0, 10)
    28  	if err != nil {
    29  		http.Error(w, err.Error(), http.StatusInternalServerError)
    30  		return
    31  	}
    32  
    33  	log.Printf("len clients: %d", len(clients))
    34  
    35  	errMsg, _ := sessionService.GetFlashMessage()
    36  	renderTemplate(w, "clients.html", map[string]interface{}{
    37  		"error":       errMsg,
    38  		"currentUser": currentUser,
    39  		"clients":     &clients,
    40  		"username":    session.Username,
    41  		"queryString": getQueryString(r.URL.Query()),
    42  	})
    43  }
    44  
    45  func (s *Service) newClientForm(w http.ResponseWriter, r *http.Request) {
    46  	sessionService, err := getSessionService(r)
    47  	if err != nil {
    48  		http.Error(w, err.Error(), http.StatusInternalServerError)
    49  		return
    50  	}
    51  
    52  	session, err := sessionService.GetUserSession()
    53  	currentUser, err := s.GetOauthService().FindUserByUsername(session.Username)
    54  	if err != nil {
    55  		http.Error(w, err.Error(), http.StatusInternalServerError)
    56  		return
    57  	}
    58  
    59  	var f createClientForm
    60  
    61  	errMsg, _ := sessionService.GetFlashMessage()
    62  	renderTemplate(w, "new_client.html", map[string]interface{}{
    63  		"error":       errMsg,
    64  		"currentUser": currentUser,
    65  		"form":        &f,
    66  		"username":    session.Username,
    67  		"queryString": getQueryString(r.URL.Query()),
    68  	})
    69  }
    70  
    71  func (s *Service) createClient(w http.ResponseWriter, r *http.Request) {
    72  	sessionService, err := getSessionService(r)
    73  	if err != nil {
    74  		http.Error(w, err.Error(), http.StatusInternalServerError)
    75  		return
    76  	}
    77  
    78  	session, err := sessionService.GetUserSession()
    79  	currentUser, err := s.GetOauthService().FindUserByUsername(session.Username)
    80  	if err != nil {
    81  		http.Error(w, err.Error(), http.StatusInternalServerError)
    82  		return
    83  	}
    84  	r.ParseForm()
    85  
    86  	var f createClientForm
    87  	err = form.ParseForm(&f, r.PostForm)
    88  	if err != nil {
    89  		http.Error(w, err.Error(), http.StatusInternalServerError)
    90  		return
    91  	}
    92  
    93  	errMsg, _ := sessionService.GetFlashMessage()
    94  	if f.Valid() {
    95  		var (
    96  			clientID = rand.String(32)
    97  			pass     = rand.String(32)
    98  		)
    99  
   100  		client, err := s.GetOauthService().CreateClientByUserID(currentUser.ID, f.Name, clientID, pass, f.RedirectURI)
   101  		if err != nil {
   102  			http.Error(w, err.Error(), http.StatusInternalServerError)
   103  			return
   104  		}
   105  		redirectWithFragment(fmt.Sprintf("/web/clients/%s", client.Key), r.URL.Query(), w, r)
   106  	} else {
   107  		renderTemplate(w, "new_client.html", map[string]interface{}{
   108  			"error":       errMsg,
   109  			"currentUser": currentUser,
   110  			"form":        &f,
   111  			"username":    session.Username,
   112  			"queryString": getQueryString(r.URL.Query()),
   113  		})
   114  	}
   115  
   116  }
   117  
   118  func (s *Service) clientDetail(w http.ResponseWriter, r *http.Request) {
   119  
   120  	sessionService, err := getSessionService(r)
   121  	if err != nil {
   122  		http.Error(w, err.Error(), http.StatusInternalServerError)
   123  		return
   124  	}
   125  
   126  	session, err := sessionService.GetUserSession()
   127  	currentUser, err := s.GetOauthService().FindUserByUsername(session.Username)
   128  	if err != nil {
   129  		http.Error(w, err.Error(), http.StatusInternalServerError)
   130  		return
   131  	}
   132  
   133  	vars := mux.Vars(r)
   134  	clientID := vars["id"]
   135  
   136  	client, err := s.GetOauthService().FindClientByClientID(clientID)
   137  	if err != nil {
   138  		http.Error(w, err.Error(), http.StatusNotFound)
   139  		return
   140  	}
   141  
   142  	errMsg, _ := sessionService.GetFlashMessage()
   143  	renderTemplate(w, "client_detail.html", map[string]interface{}{
   144  		"error":       errMsg,
   145  		"currentUser": currentUser,
   146  		"client":      client,
   147  		"username":    session.Username,
   148  		"queryString": getQueryString(r.URL.Query()),
   149  	})
   150  }
   151  
   152  func (s *Service) deleteClient(w http.ResponseWriter, r *http.Request) {
   153  }