Skip to content

Commit

Permalink
🎨 update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Flickinger committed Nov 1, 2020
1 parent 011f76b commit 3982a82
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 0 deletions.
104 changes: 104 additions & 0 deletions examples/v1/authCodeFlow/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"

"github.com/FuzzyStatic/blizzard/v1"
"github.com/FuzzyStatic/blizzard/v1/oauth"
"golang.org/x/oauth2"
)

var (
clientID string
clientSecret string
myDomain string
cfg oauth2.Config
blizz *blizzard.Client
)

func homepage(w http.ResponseWriter, r *http.Request) {
fmt.Println("Homepage Hit!")
http.Redirect(w, r, cfg.AuthCodeURL("my_random_state"), http.StatusFound)
}

func authorize(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

state := r.Form.Get("state")
if state != "my_random_state" {
http.Error(w, "State invalid", http.StatusBadRequest)
return
}

code := r.Form.Get("code")
if code == "" {
http.Error(w, "Code not found", http.StatusBadRequest)
return
}

token, err := cfg.Exchange(context.Background(), code)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

e := json.NewEncoder(w)
e.SetIndent("", " ")
err = e.Encode(*token)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

validation, _, err := blizz.TokenValidation(token)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

fmt.Printf("%+v\n", validation)

userInfo, _, err := blizz.UserInfoHeader(token)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

fmt.Printf("%+v\n", userInfo)
}

func init() {
clientID = os.Getenv("CLIENT_ID")
if clientID == "" {
log.Fatal("Set the environment variable CLIENT_ID before retrying.")
}

clientSecret = os.Getenv("CLIENT_SECRET")
if clientSecret == "" {
log.Fatal("Set the environment variable CLIENT_SECRET before retrying.")
}

myDomain = os.Getenv("MY_DOMAIN")
if clientSecret == "" {
log.Fatal("Set the environment variable MY_DOMAIN before retrying.")
}
}

func main() {
blizz = blizzard.NewClient(clientID, clientSecret, blizzard.US, blizzard.EnUS)
cfg = blizz.AuthorizeConfig(fmt.Sprintf("http://%s:9094/oauth2", myDomain), oauth.ProfileD3, oauth.ProfileSC2, oauth.ProfileWoW)

http.HandleFunc("/", homepage)
http.HandleFunc("/oauth2", authorize)

// We start up our Client on port 9094
log.Println("Client is running at 9094 port.")
log.Fatal(http.ListenAndServe(":9094", nil))
}
36 changes: 36 additions & 0 deletions examples/v1/simpleClient/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"log"
"os"

"github.com/FuzzyStatic/blizzard/v1"
)

var (
clientID string
clientSecret string
blizz *blizzard.Client
)

func init() {
clientID = os.Getenv("CLIENT_ID")
if clientID == "" {
log.Fatal("Set the environment variable CLIENT_ID before retrying.")
}

clientSecret = os.Getenv("CLIENT_SECRET")
if clientSecret == "" {
log.Fatal("Set the environment variable CLIENT_SECRET before retrying.")
}
}

func main() {
blizz = blizzard.NewClient(clientID, clientSecret, blizzard.US, blizzard.EnUS)

err := blizz.AccessTokenRequest()
if err != nil {
fmt.Println(err)
}
}
104 changes: 104 additions & 0 deletions examples/v2/authCodeFlow/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"

"github.com/FuzzyStatic/blizzard/v2"
"github.com/FuzzyStatic/blizzard/v2/oauth"
"golang.org/x/oauth2"
)

var (
clientID string
clientSecret string
myDomain string
cfg oauth2.Config
blizz *blizzard.Client
)

func homepage(w http.ResponseWriter, r *http.Request) {
fmt.Println("Homepage Hit!")
http.Redirect(w, r, cfg.AuthCodeURL("my_random_state"), http.StatusFound)
}

func authorize(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

state := r.Form.Get("state")
if state != "my_random_state" {
http.Error(w, "State invalid", http.StatusBadRequest)
return
}

code := r.Form.Get("code")
if code == "" {
http.Error(w, "Code not found", http.StatusBadRequest)
return
}

token, err := cfg.Exchange(context.Background(), code)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

e := json.NewEncoder(w)
e.SetIndent("", " ")
err = e.Encode(*token)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

validation, _, err := blizz.TokenValidation(context.Background(), token)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

fmt.Printf("%+v\n", validation)

userInfo, _, err := blizz.UserInfoHeader(token)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

fmt.Printf("%+v\n", userInfo)
}

func init() {
clientID = os.Getenv("CLIENT_ID")
if clientID == "" {
log.Fatal("Set the environment variable CLIENT_ID before retrying.")
}

clientSecret = os.Getenv("CLIENT_SECRET")
if clientSecret == "" {
log.Fatal("Set the environment variable CLIENT_SECRET before retrying.")
}

myDomain = os.Getenv("MY_DOMAIN")
if clientSecret == "" {
log.Fatal("Set the environment variable MY_DOMAIN before retrying.")
}
}

func main() {
blizz = blizzard.NewClient(clientID, clientSecret, blizzard.US, blizzard.EnUS)
cfg = blizz.AuthorizeConfig(fmt.Sprintf("http://%s:9094/oauth2", myDomain), oauth.ProfileD3, oauth.ProfileSC2, oauth.ProfileWoW)

http.HandleFunc("/", homepage)
http.HandleFunc("/oauth2", authorize)

// We start up our Client on port 9094
log.Println("Client is running at 9094 port.")
log.Fatal(http.ListenAndServe(":9094", nil))
}
37 changes: 37 additions & 0 deletions examples/v2/simpleClient/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"context"
"fmt"
"log"
"os"

"github.com/FuzzyStatic/blizzard/v2"
)

var (
clientID string
clientSecret string
blizz *blizzard.Client
)

func init() {
clientID = os.Getenv("CLIENT_ID")
if clientID == "" {
log.Fatal("Set the environment variable CLIENT_ID before retrying.")
}

clientSecret = os.Getenv("CLIENT_SECRET")
if clientSecret == "" {
log.Fatal("Set the environment variable CLIENT_SECRET before retrying.")
}
}

func main() {
blizz = blizzard.NewClient(clientID, clientSecret, blizzard.US, blizzard.EnUS)

err := blizz.AccessTokenRequest(context.Background())
if err != nil {
fmt.Println(err)
}
}

0 comments on commit 3982a82

Please sign in to comment.