Browse Source

Fix #27

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
2b23263613
  1. 15
      handlers/auth.go
  2. 12
      handlers/public.go
  3. 6
      handlers/tmpls/token.tmpl
  4. 2
      static/src/About/About.js
  5. 2
      static/src/Home/Home.js
  6. 17
      static/src/Lookup/Lookup.js
  7. 14
      static/src/index.js
  8. 13
      store/store.go
  9. 10
      store/util.go

15
handlers/auth.go

@ -24,16 +24,9 @@ type jwtClaims struct {
} }
type oAuthUser struct { type oAuthUser struct {
Sub string `json:"sub"` Sub string `json:"sub"`
Name string `json:"name"` Name string `json:"name"`
GivenName string `json:"given_name"` Picture string `json:"picture"`
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"`
} }
type checkResponse struct { type checkResponse struct {
@ -68,7 +61,6 @@ func (h *Handler) handleGoogleRedirect(c *gin.Context) {
} }
func (h *Handler) authMiddleware(c *gin.Context) { func (h *Handler) authMiddleware(c *gin.Context) {
authError := func() error { authError := func() error {
authHeader := c.GetHeader("Authorization") authHeader := c.GetHeader("Authorization")
if authHeader == "" { if authHeader == "" {
@ -83,6 +75,7 @@ func (h *Handler) authMiddleware(c *gin.Context) {
if !token.Valid { if !token.Valid {
return errors.New("token is not valid") return errors.New("token is not valid")
} }
c.Set("user", token.Claims)
return nil return nil
}() }()
if authError != nil { if authError != nil {

12
handlers/public.go

@ -27,7 +27,13 @@ func (h *Handler) handleInfo(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return 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) 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()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return return
} }
user := c.MustGet("user").(*jwtClaims)
id, err := h.store.CreateEntry(data.URL, c.ClientIP()) id, err := h.store.CreateEntry(data.URL, c.ClientIP(), user.OAuthProvider, user.OAuthID)
if err != nil { if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return return

6
handlers/tmpls/token.tmpl

@ -7,11 +7,7 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>You will be redirected</title> <title>You will be redirected</title>
<script> <script>
window.opener.dispatchEvent(new CustomEvent('onAuthCallback', { window.opener.postMessage({{ .token }}, window.location.origin)
detail: {
token: {{ .token }}
}
}));
window.close(); window.close();
</script> </script>
</head> </head>

2
static/src/About/About.js

@ -1,7 +1,7 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import { Container } from 'semantic-ui-react' import { Container } from 'semantic-ui-react'
export default class AppComponent extends Component { export default class AboutComponent extends Component {
render() { render() {
return ( return (
<Container > <Container >

2
static/src/Home/Home.js

@ -41,7 +41,7 @@ export default class HomeComponent extends Component {
<Input required size='big' ref={input => 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' /> <Input required size='big' ref={input => 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' />
</Form.Field> </Form.Field>
</Form> </Form>
</Segment > </Segment>
<Card.Group itemsPerRow="2"> <Card.Group itemsPerRow="2">
{links.map((link, i) => <Card key={i}> {links.map((link, i) => <Card key={i}>
<Card.Content> <Card.Content>

17
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 (
<Segment raised>
<Header size='huge'>URL Lookup</Header>
<Form onSubmit={this.handleURLSubmit} autoComplete="off">
<Form.Field>
<Input required size='big' ref={input => this.urlInput = input} action={{ icon: 'arrow right', labelPosition: 'right', content: 'Lookup' }} type='url' onChange={this.handleURLChange} name='url' placeholder={window.location.origin+"/..."} />
</Form.Field>
</Form>
</Segment>
)
}
};

14
static/src/index.js

@ -7,6 +7,7 @@ import 'semantic-ui-css/semantic.min.css';
import About from './About/About' import About from './About/About'
import Home from './Home/Home' import Home from './Home/Home'
import ShareX from './ShareX/ShareX' import ShareX from './ShareX/ShareX'
import Lookup from './Lookup/Lookup'
export default class BaseComponent extends Component { export default class BaseComponent extends Component {
state = { state = {
@ -51,13 +52,15 @@ export default class BaseComponent extends Component {
} }
onAuthCallback = data => { onAuthCallback = data => {
// clear the old event listener, so that the event can only emitted be once if (data.isTrusted) {
window.removeEventListener('onAuthCallback', this.onAuthCallback); // clear the old event listener, so that the event can only emitted be once
window.localStorage.setItem('token', data.detail.token); window.removeEventListener('message', this.onAuthCallback);
this.checkAuth(); window.localStorage.setItem('token', data.data);
this.checkAuth();
}
} }
onAuthClick = () => { 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 // Open the oAuth window that is it centered in the middle of the screen
var wwidth = 400, var wwidth = 400,
wHeight = 500; wHeight = 500;
@ -120,6 +123,7 @@ export default class BaseComponent extends Component {
<Route exact path="/" component={Home} /> <Route exact path="/" component={Home} />
<Route path="/about" component={About} /> <Route path="/about" component={About} />
<Route path="/ShareX" component={ShareX} /> <Route path="/ShareX" component={ShareX} />
<Route path="/Lookup" component={Lookup} />
</Container> </Container>
</HashRouter> </HashRouter>
) )

13
store/store.go

@ -23,10 +23,11 @@ type Store struct {
// Entry is the data set which is stored in the DB as JSON // Entry is the data set which is stored in the DB as JSON
type Entry struct { type Entry struct {
URL string URL string
VisitCount int VisitCount int
RemoteAddr string `json:",omitempty"` RemoteAddr string `json:",omitempty"`
CreatedOn, LastVisit time.Time OAuthProvider, OAuthID string
CreatedOn, LastVisit time.Time
} }
// ErrNoEntryFound is returned when no entry to a id is found // 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 // 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) { if !govalidator.IsURL(URL) {
return "", ErrNoValidURL return "", ErrNoValidURL
} }
// try it 10 times to make a short URL // try it 10 times to make a short URL
for i := 1; i <= 10; i++ { for i := 1; i <= 10; i++ {
id, err := s.createEntry(URL, remoteAddr) id, err := s.createEntry(URL, remoteAddr, oAuthProvider, oAuthID)
if err != nil { if err != nil {
s.log.Debugf("Could not create entry: %v", err) s.log.Debugf("Could not create entry: %v", err)
continue continue

10
store/util.go

@ -41,7 +41,7 @@ func (s *Store) createEntryRaw(key, value []byte) error {
} }
// createEntry creates a new entry // 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) id, err := generateRandomString(s.idLength)
if err != nil { if err != nil {
return "", errors.Wrap(err, "could not generate random string") 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) exists := s.checkExistence(id)
if !exists { if !exists {
raw, err := json.Marshal(Entry{ raw, err := json.Marshal(Entry{
URL: URL, URL: URL,
RemoteAddr: remoteAddr, RemoteAddr: remoteAddr,
CreatedOn: time.Now(), CreatedOn: time.Now(),
OAuthProvider: oAuthProvider,
OAuthID: oAuthID,
}) })
if err != nil { if err != nil {
return "", err return "", err

Loading…
Cancel
Save