diff --git a/handlers/auth.go b/handlers/auth.go index 3d390fa..3688fe1 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -24,16 +24,9 @@ type jwtClaims struct { } type oAuthUser struct { - Sub string `json:"sub"` - Name string `json:"name"` - GivenName string `json:"given_name"` - FamilyName string `json:"family_name"` - Profile string `json:"profile"` - Picture string `json:"picture"` - Email string `json:"email"` - EmailVerified bool `json:"email_verified"` - Gender string `json:"gender"` - Hd string `json:"hd"` + Sub string `json:"sub"` + Name string `json:"name"` + Picture string `json:"picture"` } type checkResponse struct { @@ -68,7 +61,6 @@ func (h *Handler) handleGoogleRedirect(c *gin.Context) { } func (h *Handler) authMiddleware(c *gin.Context) { - authError := func() error { authHeader := c.GetHeader("Authorization") if authHeader == "" { @@ -83,6 +75,7 @@ func (h *Handler) authMiddleware(c *gin.Context) { if !token.Valid { return errors.New("token is not valid") } + c.Set("user", token.Claims) return nil }() if authError != nil { diff --git a/handlers/public.go b/handlers/public.go index 5e2a22b..d5d3054 100644 --- a/handlers/public.go +++ b/handlers/public.go @@ -27,7 +27,13 @@ func (h *Handler) handleInfo(c *gin.Context) { c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) return } - entry.RemoteAddr = "" + user := c.MustGet("user").(*jwtClaims) + if entry.OAuthID != user.OAuthID || entry.OAuthProvider != user.OAuthProvider { + c.JSON(http.StatusOK, store.Entry{ + URL: entry.URL, + }) + return + } c.JSON(http.StatusOK, entry) } @@ -58,8 +64,8 @@ func (h *Handler) handleCreate(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - - id, err := h.store.CreateEntry(data.URL, c.ClientIP()) + user := c.MustGet("user").(*jwtClaims) + id, err := h.store.CreateEntry(data.URL, c.ClientIP(), user.OAuthProvider, user.OAuthID) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return diff --git a/handlers/tmpls/token.tmpl b/handlers/tmpls/token.tmpl index aba7558..a915640 100644 --- a/handlers/tmpls/token.tmpl +++ b/handlers/tmpls/token.tmpl @@ -7,11 +7,7 @@ You will be redirected diff --git a/static/src/About/About.js b/static/src/About/About.js index 26ed7ab..6d2977e 100644 --- a/static/src/About/About.js +++ b/static/src/About/About.js @@ -1,7 +1,7 @@ import React, { Component } from 'react' import { Container } from 'semantic-ui-react' -export default class AppComponent extends Component { +export default class AboutComponent extends Component { render() { return ( diff --git a/static/src/Home/Home.js b/static/src/Home/Home.js index 4c05d28..11ad0fe 100644 --- a/static/src/Home/Home.js +++ b/static/src/Home/Home.js @@ -41,7 +41,7 @@ export default class HomeComponent extends Component { this.urlInput = input} action={{ icon: 'arrow right', labelPosition: 'right', content: 'Shorten' }} type='url' onChange={this.handleURLChange} name='url' placeholder='Paste a link to shorten it' /> - + {links.map((link, i) => diff --git a/static/src/Lookup/Lookup.js b/static/src/Lookup/Lookup.js new file mode 100644 index 0000000..50a033f --- /dev/null +++ b/static/src/Lookup/Lookup.js @@ -0,0 +1,17 @@ +import React, { Component } from 'react' +import { Segment, Header, Form, Input } from 'semantic-ui-react' + +export default class LookupComponent extends Component { + render() { + return ( + +
URL Lookup
+
+ + this.urlInput = input} action={{ icon: 'arrow right', labelPosition: 'right', content: 'Lookup' }} type='url' onChange={this.handleURLChange} name='url' placeholder={window.location.origin+"/..."} /> + +
+
+ ) + } +}; diff --git a/static/src/index.js b/static/src/index.js index 5a6f632..af5be9e 100644 --- a/static/src/index.js +++ b/static/src/index.js @@ -7,6 +7,7 @@ import 'semantic-ui-css/semantic.min.css'; import About from './About/About' import Home from './Home/Home' import ShareX from './ShareX/ShareX' +import Lookup from './Lookup/Lookup' export default class BaseComponent extends Component { state = { @@ -51,13 +52,15 @@ export default class BaseComponent extends Component { } onAuthCallback = data => { - // clear the old event listener, so that the event can only emitted be once - window.removeEventListener('onAuthCallback', this.onAuthCallback); - window.localStorage.setItem('token', data.detail.token); - this.checkAuth(); + if (data.isTrusted) { + // clear the old event listener, so that the event can only emitted be once + window.removeEventListener('message', this.onAuthCallback); + window.localStorage.setItem('token', data.data); + this.checkAuth(); + } } onAuthClick = () => { - window.addEventListener('onAuthCallback', this.onAuthCallback, false); + window.addEventListener('message', this.onAuthCallback, false); // Open the oAuth window that is it centered in the middle of the screen var wwidth = 400, wHeight = 500; @@ -120,6 +123,7 @@ export default class BaseComponent extends Component { +
) diff --git a/store/store.go b/store/store.go index cda6505..2d77750 100644 --- a/store/store.go +++ b/store/store.go @@ -23,10 +23,11 @@ type Store struct { // Entry is the data set which is stored in the DB as JSON type Entry struct { - URL string - VisitCount int - RemoteAddr string `json:",omitempty"` - CreatedOn, LastVisit time.Time + URL string + VisitCount int + RemoteAddr string `json:",omitempty"` + OAuthProvider, OAuthID string + CreatedOn, LastVisit time.Time } // ErrNoEntryFound is returned when no entry to a id is found @@ -113,13 +114,13 @@ func (s *Store) GetEntryByIDRaw(id string) ([]byte, error) { } // CreateEntry creates a new record and returns his short id -func (s *Store) CreateEntry(URL, remoteAddr string) (string, error) { +func (s *Store) CreateEntry(URL, remoteAddr, oAuthProvider, oAuthID string) (string, error) { if !govalidator.IsURL(URL) { return "", ErrNoValidURL } // try it 10 times to make a short URL for i := 1; i <= 10; i++ { - id, err := s.createEntry(URL, remoteAddr) + id, err := s.createEntry(URL, remoteAddr, oAuthProvider, oAuthID) if err != nil { s.log.Debugf("Could not create entry: %v", err) continue diff --git a/store/util.go b/store/util.go index 30a6022..cc4bfa8 100644 --- a/store/util.go +++ b/store/util.go @@ -41,7 +41,7 @@ func (s *Store) createEntryRaw(key, value []byte) error { } // createEntry creates a new entry -func (s *Store) createEntry(URL, remoteAddr string) (string, error) { +func (s *Store) createEntry(URL, remoteAddr, oAuthProvider, oAuthID string) (string, error) { id, err := generateRandomString(s.idLength) if err != nil { return "", errors.Wrap(err, "could not generate random string") @@ -49,9 +49,11 @@ func (s *Store) createEntry(URL, remoteAddr string) (string, error) { exists := s.checkExistence(id) if !exists { raw, err := json.Marshal(Entry{ - URL: URL, - RemoteAddr: remoteAddr, - CreatedOn: time.Now(), + URL: URL, + RemoteAddr: remoteAddr, + CreatedOn: time.Now(), + OAuthProvider: oAuthProvider, + OAuthID: oAuthID, }) if err != nil { return "", err