-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapi_provider.go
95 lines (84 loc) · 2.76 KB
/
api_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
b64 "encoding/base64"
"fmt"
"html/template"
"log"
"net/http"
)
// CustData : Customers data for provider website.
type CustData struct {
ID string
PW string
Name string
Age int
Desc string
Nonce string
}
var customers []CustData
func init() {
//Init customer data in memory
customers = append(customers, []CustData{
CustData{ID: "11", PW: "pw11", Name: "Tom", Age: 43, Desc: "He is from A corp. likes to read comic books."},
CustData{ID: "22", PW: "pw22", Name: "John", Age: 25, Desc: "He is from B corp. likes to read news paper"},
CustData{ID: "33", PW: "pw33", Name: "Mary", Age: 13, Desc: "She is a student, like to read science books"},
}...)
}
// WEB: List all user in memory
func listCust(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Bookstore customer list as follow:\n")
for i, usr := range customers {
fmt.Fprintf(w, "%d \tID: %s \tName: %s \tPW: %s \tDesc:%s \n", i, usr.ID, usr.Name, usr.PW, usr.Desc)
}
}
// WEB: For login (just for demo)
func login(w http.ResponseWriter, r *http.Request) {
//7. The user enters his/her credentials.
if err := r.ParseForm(); err != nil {
log.Printf("ParseForm() err: %v\n", err)
return
}
name := r.FormValue("user")
pw := r.FormValue("pass")
token := r.FormValue("token")
for i, usr := range customers {
if usr.ID == name {
if pw == usr.PW {
//8. The web server acquires the user ID from the provider's service and uses that to generate a nonce.
sNonce := generateNonce(token, name, pw)
//update nonce to provider DB to store it.
customers[i].Nonce = sNonce
//9. The web server redirects the user to the account-linking endpoint.
//10. The user accesses the account-linking endpoint.
//Print link to user to click it.
targetURL := fmt.Sprintf("https://access.line.me/dialog/bot/accountLink?linkToken=%s&nonce=%s", token, sNonce)
log.Println("generate nonce, targetURL=", targetURL)
tmpl := template.Must(template.ParseFiles("link.tmpl"))
if err := tmpl.Execute(w, targetURL); err != nil {
log.Println("Template err:", err)
}
return
}
}
}
fmt.Fprintf(w, "Your input name or password error.")
}
// WEB: For account link
func link(w http.ResponseWriter, r *http.Request) {
//5. The user accesses the linking URL.
TOKEN := r.FormValue("linkToken")
if TOKEN == "" {
log.Println("No token.")
return
}
log.Println("token = ", TOKEN)
tmpl := template.Must(template.ParseFiles("login.tmpl"))
//6. The web server displays the login screen.
if err := tmpl.Execute(w, TOKEN); err != nil {
log.Println("Template err:", err)
}
}
// generate nonce (currently nonce combine by token + name + pw)
func generateNonce(token, name, pw string) string {
return b64.StdEncoding.EncodeToString([]byte(token + name + pw))
}